home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / CPAN.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  93.5 KB  |  3,321 lines

  1. package CPAN;
  2. use vars qw{$Try_autoload 
  3.         $META $Signal $Cwd $End $Suppress_readline %Dontload};
  4.  
  5. $VERSION = '1.27';
  6.  
  7.  
  8.  
  9. use Carp ();
  10. use Config ();
  11. use Cwd ();
  12. use DirHandle;
  13. use Exporter ();
  14. use ExtUtils::MakeMaker ();
  15. use File::Basename ();
  16. use File::Copy ();
  17. use File::Find;
  18. use File::Path ();
  19. use FileHandle ();
  20. use Safe ();
  21. use Text::ParseWords ();
  22. use Text::Wrap;
  23.  
  24. END { $End++; &cleanup; }
  25.  
  26. %CPAN::DEBUG = qw(
  27.           CPAN              1
  28.           Index             2
  29.           InfoObj           4
  30.           Author            8
  31.           Distribution     16
  32.           Bundle           32
  33.           Module           64
  34.           CacheMgr        128
  35.           Complete        256
  36.           FTP             512
  37.           Shell          1024
  38.           Eval           2048
  39.           Config         4096
  40.          );
  41.  
  42. $CPAN::DEBUG ||= 0;
  43. $CPAN::Signal ||= 0;
  44.  
  45. package CPAN;
  46. use vars qw($VERSION @EXPORT $AUTOLOAD $DEBUG $META $term);
  47. use strict qw(vars);
  48.  
  49. @CPAN::ISA = qw(CPAN::Debug Exporter MM); # the MM class from
  50.  
  51. @EXPORT = qw(
  52.          autobundle bundle expand force get
  53.          install make readme recompile shell test clean
  54.         );
  55.  
  56. sub AUTOLOAD {
  57.     my($l) = $AUTOLOAD;
  58.     $l =~ s/.*:://;
  59.     my(%EXPORT);
  60.     @EXPORT{@EXPORT} = '';
  61.     if (exists $EXPORT{$l}){
  62.     CPAN::Shell->$l(@_);
  63.     } else {
  64.     my $ok = CPAN::Shell->try_dot_al($AUTOLOAD);
  65.     if ($ok) {
  66.         goto &$AUTOLOAD;
  67.     } else {
  68.         warn "not OK: $@";
  69.     }
  70.     warn "CPAN doesn't know how to autoload $AUTOLOAD :-(
  71. Nothing Done.
  72. ";
  73.     sleep 1;
  74.     CPAN::Shell->h;
  75.     }
  76. }
  77.  
  78. sub shell {
  79.     $Suppress_readline ||= ! -t STDIN;
  80.  
  81.     my $prompt = "cpan> ";
  82.     local($^W) = 1;
  83.     unless ($Suppress_readline) {
  84.     require Term::ReadLine;
  85.     $term = Term::ReadLine->new('CPAN Monitor');
  86.     $readline::rl_completion_function =
  87.         $readline::rl_completion_function = 'CPAN::Complete::cpl';
  88.     }
  89.  
  90.     no strict;
  91.     $META->checklock();
  92.     my $getcwd;
  93.     $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  94.     my $cwd = CPAN->$getcwd();
  95.     my $rl_avail = $Suppress_readline ? "suppressed" :
  96.     ($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
  97.         "available (try ``install Bundle::CPAN'')";
  98.  
  99.     print qq{
  100. cpan shell -- CPAN exploration and modules installation (v$CPAN::VERSION)
  101. Readline support $rl_avail
  102.  
  103. } unless $CPAN::Config->{'inhibit_startup_message'} ;
  104.     while () {
  105.     if ($Suppress_readline) {
  106.         print $prompt;
  107.         last unless defined ($_ = <> );
  108.         chomp;
  109.     } else {
  110.         last unless defined ($_ = $term->readline($prompt));
  111.     }
  112.     s/^\s+//;
  113.     next if /^$/;
  114.     $_ = 'h' if $_ eq '?';
  115.     if (/^\!/) {
  116.         s/^\!//;
  117.         my($eval) = $_;
  118.         package CPAN::Eval;
  119.         use vars qw($import_done);
  120.         CPAN->import(':DEFAULT') unless $import_done++;
  121.         CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
  122.         eval($eval);
  123.         warn $@ if $@;
  124.     } elsif (/^q(?:uit)?$/i) {
  125.         last;
  126.     } elsif (/./) {
  127.         my(@line);
  128.         if ($] < 5.00322) { # parsewords had a bug until recently
  129.         @line = split;
  130.         } else {
  131.         eval { @line = Text::ParseWords::shellwords($_) };
  132.         warn($@), next if $@;
  133.         }
  134.         $CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
  135.         my $command = shift @line;
  136.         eval { CPAN::Shell->$command(@line) };
  137.         warn $@ if $@;
  138.     }
  139.     } continue {
  140.     &cleanup, die "Goodbye\n" if $Signal;
  141.     chdir $cwd;
  142.     print "\n";
  143.     }
  144. }
  145.  
  146. package CPAN::CacheMgr;
  147. use vars qw($Du);
  148. @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj);
  149. use File::Find;
  150.  
  151. package CPAN::Config;
  152. import ExtUtils::MakeMaker 'neatvalue';
  153. use vars qw(%can $dot_cpan);
  154.  
  155. %can = (
  156.   'commit' => "Commit changes to disk",
  157.   'defaults' => "Reload defaults from disk",
  158.   'init'   => "Interactive setting of all options",
  159. );
  160.  
  161. package CPAN::FTP;
  162. use vars qw($Ua);
  163. @CPAN::FTP::ISA = qw(CPAN::Debug);
  164.  
  165. package CPAN::Complete;
  166. @CPAN::Complete::ISA = qw(CPAN::Debug);
  167.  
  168. package CPAN::Index;
  169. use vars qw($last_time $date_of_03);
  170. @CPAN::Index::ISA = qw(CPAN::Debug);
  171. $last_time ||= 0;
  172. $date_of_03 ||= 0;
  173.  
  174. package CPAN::InfoObj;
  175. @CPAN::InfoObj::ISA = qw(CPAN::Debug);
  176.  
  177. package CPAN::Author;
  178. @CPAN::Author::ISA = qw(CPAN::InfoObj);
  179.  
  180. package CPAN::Distribution;
  181. @CPAN::Distribution::ISA = qw(CPAN::InfoObj);
  182.  
  183. package CPAN::Bundle;
  184. @CPAN::Bundle::ISA = qw(CPAN::Module);
  185.  
  186. package CPAN::Module;
  187. @CPAN::Module::ISA = qw(CPAN::InfoObj);
  188.  
  189. package CPAN::Shell;
  190. use vars qw($AUTOLOAD $redef @ISA);
  191. @CPAN::Shell::ISA = qw(CPAN::Debug);
  192.  
  193. sub AUTOLOAD {
  194.     my($autoload) = $AUTOLOAD;
  195.     $autoload =~ s/.*:://;
  196.     if ($autoload =~ /^w/) {
  197.     if ($CPAN::META->has_inst('CPAN::WAIT')) {
  198.         CPAN::WAIT->wh;
  199.     } else {
  200.         print STDERR qq{
  201. Commands starting with "w" require CPAN::WAIT to be installed.
  202. Please consider installing CPAN::WAIT to use the fulltext index.
  203. For this you just need to type 
  204.     install CPAN::WAIT
  205. }
  206.     }
  207.     } else {
  208.     my $ok = CPAN::Shell->try_dot_al($AUTOLOAD);
  209.     if ($ok) {
  210.         goto &$AUTOLOAD;
  211.     } else {
  212.         warn "not OK: $@";
  213.     }
  214.     warn "CPAN::Shell doesn't know how to autoload $autoload :-(
  215. Nothing Done.
  216. ";
  217.     sleep 1;
  218.     CPAN::Shell->h;
  219.     }
  220. }
  221.  
  222. sub try_dot_al {
  223.     my($class,$autoload) = @_;
  224.     return unless $CPAN::Try_autoload;
  225.     my($name,$ok);
  226.     {
  227.     my ($pkg,$func) = $autoload =~ /(.*)::([^:]+)$/;
  228.     $pkg =~ s|::|/|g;
  229.     if (defined($name=$INC{"$pkg.pm"}))
  230.         {
  231.         $name =~ s|^(.*)$pkg\.pm$|$1auto/$pkg/$func.al|;
  232.         $name = undef unless (-r $name); 
  233.         }
  234.     unless (defined $name)
  235.         {
  236.         $name = "auto/$autoload.al";
  237.         $name =~ s|::|/|g;
  238.         }
  239.     }
  240.     my $save = $@;
  241.     eval {local $SIG{__DIE__};require $name};
  242.     if ($@) {
  243.     if (substr($autoload,-9) eq '::DESTROY') {
  244.         *$autoload = sub {};
  245.         $ok = 1;
  246.     } else {
  247.         if ($name =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){
  248.         eval {local $SIG{__DIE__};require $name};
  249.         }
  250.         if ($@){
  251.         $@ =~ s/ at .*\n//;
  252.         Carp::croak $@;
  253.         } else {
  254.         $ok = 1;
  255.         }
  256.     }
  257.     } else {
  258.     $ok = 1;
  259.     }
  260.     $@ = $save;
  261.     my $lm = Carp::longmess();
  262.     return $ok;
  263. }
  264.  
  265. eval {require CPAN::WAIT;};
  266. unless ($@) {
  267.     unshift @ISA, "CPAN::WAIT";
  268. }
  269.  
  270.  
  271. if ($CPAN::Try_autoload) {
  272.     for my $p (qw(
  273.            CPAN::Author CPAN::Bundle CPAN::CacheMgr CPAN::Complete
  274.            CPAN::Config CPAN::Debug CPAN::Distribution CPAN::FTP
  275.            CPAN::FTP::netrc CPAN::Index CPAN::InfoObj CPAN::Module
  276.          )) {
  277.     *{"$p\::AUTOLOAD"} = \&AutoLoader::AUTOLOAD;
  278.     }
  279. }
  280.  
  281.  
  282. package CPAN;
  283.  
  284. $META ||= CPAN->new;                 # In case we reeval ourselves we
  285.  
  286. CPAN::Config->load unless defined $CPAN::No_Config_is_ok;
  287.  
  288. 1;
  289.  
  290.  
  291. sub autobundle;
  292. sub bundle;
  293. sub expand;
  294. sub force;
  295. sub install;
  296. sub make;
  297. sub clean;
  298. sub test;
  299.  
  300. sub all {
  301.     my($mgr,$class) = @_;
  302.     CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
  303.     CPAN::Index->reload;
  304.     values %{ $META->{$class} };
  305. }
  306.  
  307. sub checklock {
  308.     my($self) = @_;
  309.     my $lockfile = CPAN->catfile($CPAN::Config->{cpan_home},".lock");
  310.     if (-f $lockfile && -M _ > 0) {
  311.     my $fh = FileHandle->new($lockfile);
  312.     my $other = <$fh>;
  313.     $fh->close;
  314.     if (defined $other && $other) {
  315.         chomp $other;
  316.         return if $$==$other; # should never happen
  317.         print qq{There seems to be running another CPAN process }.
  318.         qq{($other). Trying to contact...\n};
  319.         if (kill 0, $other) {
  320.         Carp::croak qq{Other job is running.\n}.
  321.             qq{You may want to kill it and delete the lockfile, }.
  322.             qq{maybe. On UNIX try:\n}.
  323.             qq{    kill $other\n}.
  324.                 qq{    rm $lockfile\n};
  325.         } elsif (-w $lockfile) {
  326.         my($ans) =
  327.             ExtUtils::MakeMaker::prompt
  328.             (qq{Other job not responding. Shall I overwrite }.
  329.              qq{the lockfile? (Y/N)},"y");
  330.         print("Ok, bye\n"), exit unless $ans =~ /^y/i;
  331.         } else {
  332.         Carp::croak(
  333.                 qq{Lockfile $lockfile not writeable by you. }.
  334.                 qq{Cannot proceed.\n}.
  335.                 qq{    On UNIX try:\n}.
  336.                 qq{    rm $lockfile\n}.
  337.                 qq{  and then rerun us.\n}
  338.                );
  339.         }
  340.     }
  341.     }
  342.     File::Path::mkpath($CPAN::Config->{cpan_home});
  343.     my $fh;
  344.     unless ($fh = FileHandle->new(">$lockfile")) {
  345.     if ($! =~ /Permission/) {
  346.         my $incc = $INC{'CPAN/Config.pm'};
  347.         my $myincc = MM->catfile($ENV{HOME},'.cpan','CPAN','MyConfig.pm');
  348.         print qq{
  349.  
  350. Your configuration suggests that CPAN.pm should use a working
  351. directory of
  352.     $CPAN::Config->{cpan_home}
  353. Unfortunately we could not create the lock file
  354.     $lockfile
  355. due to permission problems.
  356.  
  357. Please make sure that the configuration variable
  358.     \$CPAN::Config->{cpan_home}
  359. points to a directory where you can write a .lock file. You can set
  360. this variable in either
  361.     $incc
  362. or
  363.     $myincc
  364.  
  365. };
  366.     }
  367.     Carp::croak "Could not open >$lockfile: $!";
  368.     }
  369.     print $fh $$, "\n";
  370.     $self->{LOCK} = $lockfile;
  371.     $fh->close;
  372.     $SIG{'TERM'} = sub { &cleanup; die "Got SIGTERM, leaving"; };
  373.     $SIG{'INT'} = sub {
  374.     my $s = $Signal == 2 ? "a second" : "another";
  375.     &cleanup, die "Got $s SIGINT" if $Signal;
  376.     $Signal = 1;
  377.     };
  378.     $SIG{'__DIE__'} = \&cleanup;
  379.     $self->debug("Signal handler set.") if $CPAN::DEBUG;
  380. }
  381.  
  382. sub DESTROY {
  383.     &cleanup; # need an eval?
  384. }
  385.  
  386. sub cwd {Cwd::cwd();}
  387.  
  388. sub getcwd {Cwd::getcwd();}
  389.  
  390. sub exists {
  391.     my($mgr,$class,$id) = @_;
  392.     CPAN::Index->reload;
  393.     $id ||= "";
  394.     exists $META->{$class}{$id};
  395. }
  396.  
  397. sub has_inst {
  398.     my($self,$mod,$message) = @_;
  399.     Carp::croak("CPAN->has_inst() called without an argument")
  400.     unless defined $mod;
  401.     if (defined $message && $message eq "no") {
  402.     $Dontload{$mod}||=1;
  403.     return 0;
  404.     } elsif (exists $Dontload{$mod}) {
  405.     return 0;
  406.     }
  407.     my $file = $mod;
  408.     $file =~ s|::|/|g;
  409.     $file =~ s|/|\\|g if $^O eq 'MSWin32';
  410.     $file .= ".pm";
  411.     if (exists $INC{$file} && $INC{$file}) {
  412.     return 1;
  413.     } elsif ( my($obj) = CPAN::Shell->expand('Module',$mod) ) {
  414.     if ($obj->inst_file) {
  415.         require $file;
  416.         print "CPAN: $mod successfully required\n";
  417.  
  418.         if ($mod eq "CPAN::WAIT") {
  419.         push @CPAN::Shell::ISA, CPAN::WAIT unless $@;
  420.         }
  421.         warn $@ if $@;
  422.         return $@ ? 0 : 1;
  423.     } elsif ($mod eq "MD5"){
  424.         print qq{
  425.   CPAN: MD5 security checks disabled because MD5 not installed.
  426.   Please consider installing the MD5 module
  427.  
  428. };
  429.         sleep 2;
  430.     }
  431.     } elsif (eval { require $file }) {
  432.     return 1;
  433.     } elsif ($mod eq "Net::FTP") {
  434.     warn qq{
  435.   Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
  436.   if you just type
  437.       install Bundle::libnet
  438.   Thank you.
  439.  
  440. };
  441.     sleep 2;
  442.     }
  443.     return 0;
  444. }
  445.  
  446. sub instance {
  447.     my($mgr,$class,$id) = @_;
  448.     CPAN::Index->reload;
  449.     $id ||= "";
  450.     $META->{$class}{$id} ||= $class->new(ID => $id );
  451. }
  452.  
  453. sub new {
  454.     bless {}, shift;
  455. }
  456.  
  457. sub cleanup {
  458.     local $SIG{__DIE__} = '';
  459.     my $i = 0; my $ineval = 0; my $sub;
  460.     while ((undef,undef,undef,$sub) = caller(++$i)) {
  461.       $ineval = 1, last if $sub eq '(eval)';
  462.     }
  463.     return if $ineval && !$End;
  464.     return unless defined $META->{'LOCK'};
  465.     return unless -f $META->{'LOCK'};
  466.     unlink $META->{'LOCK'};
  467.     print STDERR "Lockfile removed.\n";
  468. }
  469.  
  470. package CPAN::CacheMgr;
  471.  
  472. sub as_string {
  473.     eval { require Data::Dumper };
  474.     if ($@) {
  475.     return shift->SUPER::as_string;
  476.     } else {
  477.     return Data::Dumper::Dumper(shift);
  478.     }
  479. }
  480.  
  481. sub cachesize {
  482.     shift->{DU};
  483. }
  484.  
  485.  
  486.  
  487. sub dir {
  488.     shift->{ID};
  489. }
  490.  
  491. sub entries {
  492.     my($self,$dir) = @_;
  493.     return unless defined $dir;
  494.     $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
  495.     $dir ||= $self->{ID};
  496.     my $getcwd;
  497.     $getcwd  = $CPAN::Config->{'getcwd'} || 'cwd';
  498.     my($cwd) = CPAN->$getcwd();
  499.     chdir $dir or Carp::croak("Can't chdir to $dir: $!");
  500.     my $dh = DirHandle->new(".") or Carp::croak("Couldn't opendir $dir: $!");
  501.     my(@entries);
  502.     for ($dh->read) {
  503.     next if $_ eq "." || $_ eq "..";
  504.     if (-f $_) {
  505.         push @entries, $CPAN::META->catfile($dir,$_);
  506.     } elsif (-d _) {
  507.         push @entries, $CPAN::META->catdir($dir,$_);
  508.     } else {
  509.         print STDERR "Warning: weird direntry in $dir: $_\n";
  510.     }
  511.     }
  512.     chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
  513.     sort { -M $b <=> -M $a} @entries;
  514. }
  515.  
  516. sub disk_usage {
  517.     my($self,$dir) = @_;
  518.     return if $self->{SIZE}{$dir};
  519.     local($Du) = 0;
  520.     find(
  521.      sub {
  522.          return if -l $_;
  523.          $Du += -s _;
  524.      },
  525.      $dir
  526.     );
  527.     $self->{SIZE}{$dir} = $Du/1024/1024;
  528.     push @{$self->{FIFO}}, $dir;
  529.     $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
  530.     $self->{DU} += $Du/1024/1024;
  531.     if ($self->{DU} > $self->{'MAX'} ) {
  532.     my($toremove) = shift @{$self->{FIFO}};
  533.     printf "...Hold on a sec... cleaning from cache (%.1f>%.1f MB): $toremove\n",
  534.         $self->{DU}, $self->{'MAX'};
  535.     $self->force_clean_cache($toremove);
  536.     }
  537.     $self->{DU};
  538. }
  539.  
  540. sub force_clean_cache {
  541.     my($self,$dir) = @_;
  542.     $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
  543.     if $CPAN::DEBUG;
  544.     File::Path::rmtree($dir);
  545.     $self->{DU} -= $self->{SIZE}{$dir};
  546.     delete $self->{SIZE}{$dir};
  547. }
  548.  
  549. sub new {
  550.     my $class = shift;
  551.     my $time = time;
  552.     my($debug,$t2);
  553.     $debug = "";
  554.     my $self = {
  555.         ID => $CPAN::Config->{'build_dir'},
  556.         MAX => $CPAN::Config->{'build_cache'},
  557.         DU => 0
  558.            };
  559.     File::Path::mkpath($self->{ID});
  560.     my $dh = DirHandle->new($self->{ID});
  561.     bless $self, $class;
  562.     $self->debug("dir [$self->{ID}]") if $CPAN::DEBUG;
  563.     my $e;
  564.     for $e ($self->entries) {
  565.     next if $e eq ".." || $e eq ".";
  566.     $self->disk_usage($e);
  567.     }
  568.     $t2 = time;
  569.     $debug .= "timing of CacheMgr->new: ".($t2 - $time);
  570.     $time = $t2;
  571.     CPAN->debug($debug) if $CPAN::DEBUG;
  572.     $self;
  573. }
  574.  
  575. package CPAN::Debug;
  576.  
  577. sub debug {
  578.     my($self,$arg) = @_;
  579.     my($caller,$func,$line,@rest) = caller(1); # caller(0) eg
  580.     ($caller) = caller(0);
  581.     $caller =~ s/.*:://;
  582.     $arg = "" unless defined $arg;
  583.     my $rest = join ":", map { defined $_ ? $_ : "UNDEF" } @rest;
  584.     if ($CPAN::DEBUG{$caller} & $CPAN::DEBUG){
  585.     if ($arg and ref $arg) {
  586.         eval { require Data::Dumper };
  587.         if ($@) {
  588.         print $arg->as_string;
  589.         } else {
  590.         print Data::Dumper::Dumper($arg);
  591.         }
  592.     } else {
  593.         print "Debug($caller:$func,$line,[$rest]): $arg\n"
  594.     }
  595.     }
  596. }
  597.  
  598. package CPAN::Config;
  599.  
  600. sub edit {
  601.     my($class,@args) = @_;
  602.     return unless @args;
  603.     CPAN->debug("class[$class]args[".join(" | ",@args)."]");
  604.     my($o,$str,$func,$args,$key_exists);
  605.     $o = shift @args;
  606.     if($can{$o}) {
  607.     $class->$o(@args);
  608.     return 1;
  609.     } else {
  610.     if (ref($CPAN::Config->{$o}) eq ARRAY) {
  611.         $func = shift @args;
  612.         $func ||= "";
  613.         if ($func eq "push") {
  614.         push @{$CPAN::Config->{$o}}, @args;
  615.         } elsif ($func eq "pop") {
  616.         pop @{$CPAN::Config->{$o}};
  617.         } elsif ($func eq "shift") {
  618.         shift @{$CPAN::Config->{$o}};
  619.         } elsif ($func eq "unshift") {
  620.         unshift @{$CPAN::Config->{$o}}, @args;
  621.         } elsif ($func eq "splice") {
  622.         splice @{$CPAN::Config->{$o}}, @args;
  623.         } elsif (@args) {
  624.         $CPAN::Config->{$o} = [@args];
  625.         } else {
  626.         print(
  627.               "  $o  ",
  628.               ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$o}),
  629.               "\n"
  630.              );
  631.         }
  632.     } else {
  633.         $CPAN::Config->{$o} = $args[0] if defined $args[0];
  634.         print "    $o    ";
  635.         print defined $CPAN::Config->{$o} ?
  636.         $CPAN::Config->{$o} : "UNDEFINED";
  637.     }
  638.     }
  639. }
  640.  
  641. sub commit {
  642.     my($self,$configpm) = @_;
  643.     unless (defined $configpm){
  644.     $configpm ||= $INC{"CPAN/MyConfig.pm"};
  645.     $configpm ||= $INC{"CPAN/Config.pm"};
  646.     $configpm || Carp::confess(qq{
  647. CPAN::Config::commit called without an argument.
  648. Please specify a filename where to save the configuration or try
  649. "o conf init" to have an interactive course through configing.
  650. });
  651.     }
  652.     my($mode);
  653.     if (-f $configpm) {
  654.     $mode = (stat $configpm)[2];
  655.     if ($mode && ! -w _) {
  656.         Carp::confess("$configpm is not writable");
  657.     }
  658.     }
  659.  
  660.     my $msg = <<EOF unless $configpm =~ /MyConfig/;
  661.  
  662.  
  663. EOF
  664.     $msg ||= "\n";
  665.     my($fh) = FileHandle->new;
  666.     open $fh, ">$configpm" or warn "Couldn't open >$configpm: $!";
  667.     print $fh qq[$msg\$CPAN::Config = \{\n];
  668.     foreach (sort keys %$CPAN::Config) {
  669.     $fh->print(
  670.            "  '$_' => ",
  671.            ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
  672.            ",\n"
  673.           );
  674.     }
  675.  
  676.     print $fh "};\n1;\n__END__\n";
  677.     close $fh;
  678.  
  679.     print "commit: wrote $configpm\n";
  680.     1;
  681. }
  682.  
  683. *default = \&defaults;
  684. sub defaults {
  685.     my($self) = @_;
  686.     $self->unload;
  687.     $self->load;
  688.     1;
  689. }
  690.  
  691. sub init {
  692.     my($self) = @_;
  693.     undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
  694.     $self->load;
  695.     1;
  696. }
  697.  
  698. sub load {
  699.     my($self) = shift;
  700.     my(@miss);
  701.     eval {require CPAN::Config;};       # We eval, because of some MakeMaker problems
  702.     unshift @INC, $CPAN::META->catdir($ENV{HOME},".cpan") unless $dot_cpan++;
  703.     eval {require CPAN::MyConfig;};     # where you can override system wide settings
  704.     return unless @miss = $self->not_loaded;
  705.     require CPAN::FirstTime;
  706.     my($configpm,$fh,$redo,$theycalled);
  707.     $redo ||= "";
  708.     $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
  709.     if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
  710.     $configpm = $INC{"CPAN/Config.pm"};
  711.     $redo++;
  712.     } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
  713.     $configpm = $INC{"CPAN/MyConfig.pm"};
  714.     $redo++;
  715.     } else {
  716.     my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
  717.     my($configpmdir) = MM->catdir($path_to_cpan,"CPAN");
  718.     my($configpmtest) = MM->catfile($configpmdir,"Config.pm");
  719.     if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
  720.         if (-w $configpmtest) {
  721.         $configpm = $configpmtest;
  722.         } elsif (-w $configpmdir) {
  723.         unlink "$configpmtest.bak" if -f "$configpmtest.bak";
  724.         rename $configpmtest, "$configpmtest.bak" if -f $configpmtest;
  725.         my $fh = FileHandle->new;
  726.         if ($fh->open(">$configpmtest")) {
  727.             $fh->print("1;\n");
  728.             $configpm = $configpmtest;
  729.         } else {
  730.             Carp::confess("Cannot open >$configpmtest");
  731.         }
  732.         }
  733.     }
  734.     unless ($configpm) {
  735.         $configpmdir = MM->catdir($ENV{HOME},".cpan","CPAN");
  736.         File::Path::mkpath($configpmdir);
  737.         $configpmtest = MM->catfile($configpmdir,"MyConfig.pm");
  738.         if (-w $configpmtest) {
  739.         $configpm = $configpmtest;
  740.         } elsif (-w $configpmdir) {
  741.         my $fh = FileHandle->new;
  742.         if ($fh->open(">$configpmtest")) {
  743.             $fh->print("1;\n");
  744.             $configpm = $configpmtest;
  745.         } else {
  746.             Carp::confess("Cannot open >$configpmtest");
  747.         }
  748.         } else {
  749.         Carp::confess(qq{WARNING: CPAN.pm is unable to }.
  750.                   qq{create a configuration file.});
  751.         }
  752.     }
  753.     }
  754.     local($") = ", ";
  755.     print qq{
  756. We have to reconfigure CPAN.pm due to following uninitialized parameters:
  757.  
  758. @miss
  759. } if $redo && ! $theycalled;
  760.     print qq{
  761. $configpm initialized.
  762. };
  763.     sleep 2;
  764.     CPAN::FirstTime::init($configpm);
  765. }
  766.  
  767. sub not_loaded {
  768.     my(@miss);
  769.     for (qw(
  770.         cpan_home keep_source_where build_dir build_cache index_expire
  771.         gzip tar unzip make pager makepl_arg make_arg make_install_arg
  772.         urllist inhibit_startup_message ftp_proxy http_proxy no_proxy
  773.        )) {
  774.     push @miss, $_ unless defined $CPAN::Config->{$_};
  775.     }
  776.     return @miss;
  777. }
  778.  
  779. sub unload {
  780.     delete $INC{'CPAN/MyConfig.pm'};
  781.     delete $INC{'CPAN/Config.pm'};
  782. }
  783.  
  784. *h = \&help;
  785. sub help {
  786.     print <<EOF;
  787. Known options:
  788.   defaults  reload default config values from disk
  789.   commit    commit session changes to disk
  790.   init      go through a dialog to set all parameters
  791.  
  792. You may edit key values in the follow fashion:
  793.  
  794.   o conf build_cache 15
  795.  
  796.   o conf build_dir "/foo/bar"
  797.  
  798.   o conf urllist shift
  799.  
  800.   o conf urllist unshift ftp://ftp.foo.bar/
  801.  
  802. EOF
  803.     undef; #don't reprint CPAN::Config
  804. }
  805.  
  806. sub cpl {
  807.     my($word,$line,$pos) = @_;
  808.     $word ||= "";
  809.     my(@o_conf) = (keys %CPAN::Config::can, keys %$CPAN::Config);
  810.     return grep /^\Q$word\E/, @o_conf;
  811. }
  812.  
  813. package CPAN::Shell;
  814.  
  815. sub h {
  816.     my($class,$about) = @_;
  817.     if (defined $about) {
  818.     print "Detailed help not yet implemented\n";
  819.     } else {
  820.     print q{
  821. command   arguments       description
  822. a         string                  authors
  823. b         or              display bundles
  824. d         /regex/         info    distributions
  825. m         or              about   modules
  826. i         none                    anything of above
  827.  
  828. r          as             reinstall recommendations
  829. u          above          uninstalled distributions
  830. See manpage for autobundle, recompile, force, look, etc.
  831.  
  832. make                      make
  833. test      modules,        make test (implies make)
  834. install   dists, bundles, make install (implies test)
  835. clean     "r" or "u"      make clean
  836. readme                    display the README file
  837.  
  838. reload    index|cpan    load most recent indices/CPAN.pm
  839. h or ?                  display this menu
  840. o         various       set and query options
  841. !         perl-code     eval a perl command
  842. q                       quit the shell subroutine
  843. };
  844.     }
  845. }
  846.  
  847. sub a { print shift->format_result('Author',@_);}
  848. sub b {
  849.     my($self,@which) = @_;
  850.     CPAN->debug("which[@which]") if $CPAN::DEBUG;
  851.     my($incdir,$bdir,$dh);
  852.     foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  853.     $bdir = $CPAN::META->catdir($incdir,"Bundle");
  854.     if ($dh = DirHandle->new($bdir)) { # may fail
  855.         my($entry);
  856.         for $entry ($dh->read) {
  857.         next if -d $CPAN::META->catdir($bdir,$entry);
  858.         next unless $entry =~ s/\.pm$//;
  859.         $CPAN::META->instance('CPAN::Bundle',"Bundle::$entry");
  860.         }
  861.     }
  862.     }
  863.     print $self->format_result('Bundle',@which);
  864. }
  865. sub d { print shift->format_result('Distribution',@_);}
  866. sub m { print shift->format_result('Module',@_);}
  867.  
  868. sub i {
  869.     my($self) = shift;
  870.     my(@args) = @_;
  871.     my(@type,$type,@m);
  872.     @type = qw/Author Bundle Distribution Module/;
  873.     @args = '/./' unless @args;
  874.     my(@result);
  875.     for $type (@type) {
  876.     push @result, $self->expand($type,@args);
  877.     }
  878.     my $result =  @result == 1 ?
  879.     $result[0]->as_string :
  880.         join "", map {$_->as_glimpse} @result;
  881.     $result ||= "No objects found of any type for argument @args\n";
  882.     print $result;
  883. }
  884.  
  885. sub o {
  886.     my($self,$o_type,@o_what) = @_;
  887.     $o_type ||= "";
  888.     CPAN->debug("o_type[$o_type] o_what[".join(" | ",@o_what)."]\n");
  889.     if ($o_type eq 'conf') {
  890.     shift @o_what if @o_what && $o_what[0] eq 'help';
  891.     if (!@o_what) {
  892.         my($k,$v);
  893.         print "CPAN::Config options:\n";
  894.         for $k (sort keys %CPAN::Config::can) {
  895.         $v = $CPAN::Config::can{$k};
  896.         printf "    %-18s %s\n", $k, $v;
  897.         }
  898.         print "\n";
  899.         for $k (sort keys %$CPAN::Config) {
  900.         $v = $CPAN::Config->{$k};
  901.         if (ref $v) {
  902.             printf "    %-18s\n", $k;
  903.             print map {"\t$_\n"} @{$v};
  904.         } else {
  905.             printf "    %-18s %s\n", $k, $v;
  906.         }
  907.         }
  908.         print "\n";
  909.     } elsif (!CPAN::Config->edit(@o_what)) {
  910.         print qq[Type 'o conf' to view configuration edit options\n\n];
  911.     }
  912.     } elsif ($o_type eq 'debug') {
  913.     my(%valid);
  914.     @o_what = () if defined $o_what[0] && $o_what[0] =~ /help/i;
  915.     if (@o_what) {
  916.         while (@o_what) {
  917.         my($what) = shift @o_what;
  918.         if ( exists $CPAN::DEBUG{$what} ) {
  919.             $CPAN::DEBUG |= $CPAN::DEBUG{$what};
  920.         } elsif ($what =~ /^\d/) {
  921.             $CPAN::DEBUG = $what;
  922.         } elsif (lc $what eq 'all') {
  923.             my($max) = 0;
  924.             for (values %CPAN::DEBUG) {
  925.             $max += $_;
  926.             }
  927.             $CPAN::DEBUG = $max;
  928.         } else {
  929.             my($known) = 0;
  930.             for (keys %CPAN::DEBUG) {
  931.             next unless lc($_) eq lc($what);
  932.             $CPAN::DEBUG |= $CPAN::DEBUG{$_};
  933.             $known = 1;
  934.             }
  935.             print "unknown argument [$what]\n" unless $known;
  936.         }
  937.         }
  938.     } else {
  939.         print "Valid options for debug are ".
  940.         join(", ",sort(keys %CPAN::DEBUG), 'all').
  941.             qq{ or a number. Completion works on the options. }.
  942.             qq{Case is ignored.\n\n};
  943.     }
  944.     if ($CPAN::DEBUG) {
  945.         print "Options set for debugging:\n";
  946.         my($k,$v);
  947.         for $k (sort {$CPAN::DEBUG{$a} <=> $CPAN::DEBUG{$b}} keys %CPAN::DEBUG) {
  948.         $v = $CPAN::DEBUG{$k};
  949.         printf "    %-14s(%s)\n", $k, $v if $v & $CPAN::DEBUG;
  950.         }
  951.     } else {
  952.         print "Debugging turned off completely.\n";
  953.     }
  954.     } else {
  955.     print qq{
  956. Known options:
  957.   conf    set or get configuration variables
  958.   debug   set or get debugging options
  959. };
  960.     }
  961. }
  962.  
  963. sub reload {
  964.     my($self,$command,@arg) = @_;
  965.     $command ||= "";
  966.     $self->debug("self[$self]command[$command]arg[@arg]") if $CPAN::DEBUG;
  967.     if ($command =~ /cpan/i) {
  968.     CPAN->debug("reloading the whole CPAN.pm") if $CPAN::DEBUG;
  969.     my $fh = FileHandle->new($INC{'CPAN.pm'});
  970.     local($/);
  971.     undef $/;
  972.     $redef = 0;
  973.     local($SIG{__WARN__})
  974.         = sub {
  975.         if ( $_[0] =~ /Subroutine \w+ redefined/ ) {
  976.             ++$redef;
  977.             local($|) = 1;
  978.             print ".";
  979.             return;
  980.         }
  981.         warn @_;
  982.         };
  983.     eval <$fh>;
  984.     warn $@ if $@;
  985.     print "\n$redef subroutines redefined\n";
  986.     } elsif ($command =~ /index/) {
  987.     CPAN::Index->force_reload;
  988.     } else {
  989.     print qq{cpan     re-evals the CPAN.pm file\n};
  990.     print qq{index    re-reads the index files\n};
  991.     }
  992. }
  993.  
  994. sub _binary_extensions {
  995.     my($self) = shift @_;
  996.     my(@result,$module,%seen,%need,$headerdone);
  997.     for $module ($self->expand('Module','/./')) {
  998.     my $file  = $module->cpan_file;
  999.     next if $file eq "N/A";
  1000.     next if $file =~ /^Contact Author/;
  1001.     next if $file =~ /perl5[._-]\d{3}(?:[\d_]+)?\.tar[._-]gz$/;
  1002.     next unless $module->xs_file;
  1003.     local($|) = 1;
  1004.     print ".";
  1005.     push @result, $module;
  1006.     }
  1007.     print "\n";
  1008.     return @result;
  1009. }
  1010.  
  1011. sub recompile {
  1012.     my($self) = shift @_;
  1013.     my($module,@module,$cpan_file,%dist);
  1014.     @module = $self->_binary_extensions();
  1015.     for $module (@module){  # we force now and compile later, so we don't do it twice
  1016.     $cpan_file = $module->cpan_file;
  1017.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1018.     $pack->force;
  1019.     $dist{$cpan_file}++;
  1020.     }
  1021.     for $cpan_file (sort keys %dist) {
  1022.     print "  CPAN: Recompiling $cpan_file\n\n";
  1023.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1024.     $pack->install;
  1025.     $CPAN::Signal = 0; # it's tempting to reset Signal, so we can
  1026.     }
  1027. }
  1028.  
  1029. sub _u_r_common {
  1030.     my($self) = shift @_;
  1031.     my($what) = shift @_;
  1032.     CPAN->debug("self[$self] what[$what] args[@_]") if $CPAN::DEBUG;
  1033.     Carp::croak "Usage: \$obj->_u_r_common($what)" unless defined $what;
  1034.     Carp::croak "Usage: \$obj->_u_r_common(a|r|u)" unless $what =~ /^[aru]$/;
  1035.     my(@args) = @_;
  1036.     @args = '/./' unless @args;
  1037.     my(@result,$module,%seen,%need,$headerdone,$version_zeroes);
  1038.     $version_zeroes = 0;
  1039.     my $sprintf = "%-25s %9s %9s  %s\n";
  1040.     for $module ($self->expand('Module',@args)) {
  1041.     my $file  = $module->cpan_file;
  1042.     next unless defined $file; # ??
  1043.     my($latest) = $module->cpan_version || 0;
  1044.     my($inst_file) = $module->inst_file;
  1045.     my($have);
  1046.     if ($inst_file){
  1047.         if ($what eq "a") {
  1048.         $have = $module->inst_version;
  1049.         } elsif ($what eq "r") {
  1050.         $have = $module->inst_version;
  1051.         local($^W) = 0;
  1052.         $version_zeroes++ unless $have;
  1053.         next if $have >= $latest;
  1054.         } elsif ($what eq "u") {
  1055.         next;
  1056.         }
  1057.     } else {
  1058.         if ($what eq "a") {
  1059.         next;
  1060.         } elsif ($what eq "r") {
  1061.         next;
  1062.         } elsif ($what eq "u") {
  1063.         $have = "-";
  1064.         }
  1065.     }
  1066.     return if $CPAN::Signal; # this is sometimes lengthy
  1067.     $seen{$file} ||= 0;
  1068.     if ($what eq "a") {
  1069.         push @result, sprintf "%s %s\n", $module->id, $have;
  1070.     } elsif ($what eq "r") {
  1071.         push @result, $module->id;
  1072.         next if $seen{$file}++;
  1073.     } elsif ($what eq "u") {
  1074.         push @result, $module->id;
  1075.         next if $seen{$file}++;
  1076.         next if $file =~ /^Contact/;
  1077.     }
  1078.     unless ($headerdone++){
  1079.         print "\n";
  1080.         printf(
  1081.            $sprintf,
  1082.            "Package namespace",
  1083.            "installed",
  1084.            "latest",
  1085.            "in CPAN file"
  1086.            );
  1087.     }
  1088.     $latest = substr($latest,0,8) if length($latest) > 8;
  1089.     $have = substr($have,0,8) if length($have) > 8;
  1090.     printf $sprintf, $module->id, $have, $latest, $file;
  1091.     $need{$module->id}++;
  1092.     }
  1093.     unless (%need) {
  1094.     if ($what eq "u") {
  1095.         print "No modules found for @args\n";
  1096.     } elsif ($what eq "r") {
  1097.         print "All modules are up to date for @args\n";
  1098.     }
  1099.     }
  1100.     if ($what eq "r" && $version_zeroes) {
  1101.     my $s = $version_zeroes > 1 ? "s have" : " has";
  1102.     print qq{$version_zeroes installed module$s no version number to compare\n};
  1103.     }
  1104.     @result;
  1105. }
  1106.  
  1107. sub r {
  1108.     shift->_u_r_common("r",@_);
  1109. }
  1110.  
  1111. sub u {
  1112.     shift->_u_r_common("u",@_);
  1113. }
  1114.  
  1115. sub autobundle {
  1116.     my($self) = shift;
  1117.     my(@bundle) = $self->_u_r_common("a",@_);
  1118.     my($todir) = $CPAN::META->catdir($CPAN::Config->{'cpan_home'},"Bundle");
  1119.     File::Path::mkpath($todir);
  1120.     unless (-d $todir) {
  1121.     print "Couldn't mkdir $todir for some reason\n";
  1122.     return;
  1123.     }
  1124.     my($y,$m,$d) =  (localtime)[5,4,3];
  1125.     $y+=1900;
  1126.     $m++;
  1127.     my($c) = 0;
  1128.     my($me) = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, $c;
  1129.     my($to) = $CPAN::META->catfile($todir,"$me.pm");
  1130.     while (-f $to) {
  1131.     $me = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, ++$c;
  1132.     $to = $CPAN::META->catfile($todir,"$me.pm");
  1133.     }
  1134.     my($fh) = FileHandle->new(">$to") or Carp::croak "Can't open >$to: $!";
  1135.     $fh->print(
  1136.            "package Bundle::$me;\n\n",
  1137.            "\$VERSION = '0.01';\n\n",
  1138.            "1;\n\n",
  1139.            "__END__\n\n",
  1140.            "=head1 NAME\n\n",
  1141.            "Bundle::$me - Snapshot of installation on ",
  1142.            $Config::Config{'myhostname'},
  1143.            " on ",
  1144.            scalar(localtime),
  1145.            "\n\n=head1 SYNOPSIS\n\n",
  1146.            "perl -MCPAN -e 'install Bundle::$me'\n\n",
  1147.            "=head1 CONTENTS\n\n",
  1148.            join("\n", @bundle),
  1149.            "\n\n=head1 CONFIGURATION\n\n",
  1150.            Config->myconfig,
  1151.            "\n\n=head1 AUTHOR\n\n",
  1152.            "This Bundle has been generated automatically ",
  1153.            "by the autobundle routine in CPAN.pm.\n",
  1154.           );
  1155.     $fh->close;
  1156.     print "\nWrote bundle file
  1157.     $to\n\n";
  1158. }
  1159.  
  1160. sub expand {
  1161.     shift;
  1162.     my($type,@args) = @_;
  1163.     my($arg,@m);
  1164.     for $arg (@args) {
  1165.     my $regex;
  1166.     if ($arg =~ m|^/(.*)/$|) {
  1167.         $regex = $1;
  1168.     }
  1169.     my $class = "CPAN::$type";
  1170.     my $obj;
  1171.     if (defined $regex) {
  1172.         for $obj ( sort {$a->id cmp $b->id} $CPAN::META->all($class)) {
  1173.         push @m, $obj
  1174.             if
  1175.             $obj->id =~ /$regex/i
  1176.                 or
  1177.             (
  1178.              (
  1179.               $] < 5.00303 ### provide sort of compatibility with 5.003
  1180.               ||
  1181.               $obj->can('name')
  1182.              )
  1183.              &&
  1184.              $obj->name  =~ /$regex/i
  1185.             );
  1186.         }
  1187.     } else {
  1188.         my($xarg) = $arg;
  1189.         if ( $type eq 'Bundle' ) {
  1190.         $xarg =~ s/^(Bundle::)?(.*)/Bundle::$2/;
  1191.         }
  1192.         if ($CPAN::META->exists($class,$xarg)) {
  1193.         $obj = $CPAN::META->instance($class,$xarg);
  1194.         } elsif ($CPAN::META->exists($class,$arg)) {
  1195.         $obj = $CPAN::META->instance($class,$arg);
  1196.         } else {
  1197.         next;
  1198.         }
  1199.         push @m, $obj;
  1200.     }
  1201.     }
  1202.     return wantarray ? @m : $m[0];
  1203. }
  1204.  
  1205. sub format_result {
  1206.     my($self) = shift;
  1207.     my($type,@args) = @_;
  1208.     @args = '/./' unless @args;
  1209.     my(@result) = $self->expand($type,@args);
  1210.     my $result =  @result == 1 ?
  1211.     $result[0]->as_string :
  1212.         join "", map {$_->as_glimpse} @result;
  1213.     $result ||= "No objects of type $type found for argument @args\n";
  1214.     $result;
  1215. }
  1216.  
  1217. sub rematein {
  1218.     shift;
  1219.     my($meth,@some) = @_;
  1220.     my $pragma = "";
  1221.     if ($meth eq 'force') {
  1222.     $pragma = $meth;
  1223.     $meth = shift @some;
  1224.     }
  1225.     CPAN->debug("pragma[$pragma]meth[$meth] some[@some]") if $CPAN::DEBUG;
  1226.     my($s,@s);
  1227.     foreach $s (@some) {
  1228.     my $obj;
  1229.     if (ref $s) {
  1230.         $obj = $s;
  1231.     } elsif ($s =~ m|/|) { # looks like a file
  1232.         $obj = $CPAN::META->instance('CPAN::Distribution',$s);
  1233.     } elsif ($s =~ m|^Bundle::|) {
  1234.         $obj = $CPAN::META->instance('CPAN::Bundle',$s);
  1235.     } else {
  1236.         $obj = $CPAN::META->instance('CPAN::Module',$s)
  1237.         if $CPAN::META->exists('CPAN::Module',$s);
  1238.     }
  1239.     if (ref $obj) {
  1240.         CPAN->debug(
  1241.             qq{pragma[$pragma] meth[$meth] obj[$obj] as_string\[}.
  1242.             $obj->as_string.
  1243.             qq{\]}
  1244.                ) if $CPAN::DEBUG;
  1245.         $obj->$pragma()
  1246.         if
  1247.             $pragma
  1248.             &&
  1249.             ($] < 5.00303 || $obj->can($pragma)); ### compatibility with 5.003
  1250.         $obj->$meth();
  1251.     } elsif ($CPAN::META->exists('CPAN::Author',$s)) {
  1252.         $obj = $CPAN::META->instance('CPAN::Author',$s);
  1253.         print "Don't be silly, you can't $meth ", $obj->fullname, " ;-)\n";
  1254.     } else {
  1255.         print qq{Warning: Cannot $meth $s, don\'t know what it is.
  1256. Try the command
  1257.  
  1258.     i /$s/
  1259.  
  1260. to find objects with similar identifiers.
  1261. };
  1262.     }
  1263.     }
  1264. }
  1265.  
  1266. sub force   { shift->rematein('force',@_); }
  1267. sub get     { shift->rematein('get',@_); }
  1268. sub readme  { shift->rematein('readme',@_); }
  1269. sub make    { shift->rematein('make',@_); }
  1270. sub test    { shift->rematein('test',@_); }
  1271. sub install { shift->rematein('install',@_); }
  1272. sub clean   { shift->rematein('clean',@_); }
  1273. sub look   { shift->rematein('look',@_); }
  1274.  
  1275. package CPAN::FTP;
  1276.  
  1277. sub ftp_get {
  1278.     my($class,$host,$dir,$file,$target) = @_;
  1279.     $class->debug(
  1280.                qq[Going to fetch file [$file] from dir [$dir]
  1281.     on host [$host] as local [$target]\n]
  1282.               ) if $CPAN::DEBUG;
  1283.     my $ftp = Net::FTP->new($host);
  1284.     return 0 unless defined $ftp;
  1285.     $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
  1286.     $class->debug(qq[Going to ->login("anonymous","$Config::Config{'cf_email'}")\n]);
  1287.     unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ){
  1288.     warn "Couldn't login on $host";
  1289.     return;
  1290.     }
  1291.     unless ( $ftp->cwd($dir) ){
  1292.     warn "Couldn't cwd $dir";
  1293.     return;
  1294.     }
  1295.     $ftp->binary;
  1296.     $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
  1297.     unless ( $ftp->get($file,$target) ){
  1298.     warn "Couldn't fetch $file from $host\n";
  1299.     return;
  1300.     }
  1301.     $ftp->quit; # it's ok if this fails
  1302.     return 1;
  1303. }
  1304.  
  1305. sub localize {
  1306.     my($self,$file,$aslocal,$force) = @_;
  1307.     $force ||= 0;
  1308.     Carp::croak "Usage: ->localize(cpan_file,as_local_file[,$force])"
  1309.     unless defined $aslocal;
  1310.     $self->debug("file[$file] aslocal[$aslocal] force[$force]")
  1311.     if $CPAN::DEBUG;
  1312.  
  1313.     return $aslocal if -f $aslocal && -r _ && ! $force;
  1314.     my($restore) = 0;
  1315.     if (-f $aslocal){
  1316.     rename $aslocal, "$aslocal.bak";
  1317.     $restore++;
  1318.     }
  1319.  
  1320.     my($aslocal_dir) = File::Basename::dirname($aslocal);
  1321.     File::Path::mkpath($aslocal_dir);
  1322.     print STDERR qq{Warning: You are not allowed to write into }.
  1323.     qq{directory "$aslocal_dir".
  1324.     I\'ll continue, but if you face any problems, they may be due
  1325.     to insufficient permissions.\n} unless -w $aslocal_dir;
  1326.  
  1327.     if ($CPAN::META->has_inst('LWP')) {
  1328.     require LWP::UserAgent;
  1329.      unless ($Ua) {
  1330.         $Ua = LWP::UserAgent->new;
  1331.         my($var);
  1332.         $Ua->proxy('ftp',  $var)
  1333.         if $var = $CPAN::Config->{'ftp_proxy'} || $ENV{'ftp_proxy'};
  1334.         $Ua->proxy('http', $var)
  1335.         if $var = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
  1336.         $Ua->no_proxy($var)
  1337.         if $var = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
  1338.     }
  1339.     }
  1340.  
  1341.     my($i);
  1342.     for $i (0..$#{$CPAN::Config->{urllist}}) {
  1343.     my $url = $CPAN::Config->{urllist}[$i];
  1344.     $url .= "/" unless substr($url,-1) eq "/";
  1345.     $url .= $file;
  1346.     $self->debug("localizing[$url]") if $CPAN::DEBUG;
  1347.     if ($url =~ /^file:/) {
  1348.         my $l;
  1349.         if ($CPAN::META->has_inst('LWP')) {
  1350.         require URI::URL;
  1351.         my $u =  URI::URL->new($url);
  1352.         $l = $u->path;
  1353.         } else { # works only on Unix, is poorly constructed, but
  1354.         ($l = $url) =~ s,^file://[^/]+,,; # discard the host part
  1355.         $l =~ s/^file://;    # assume they meant file://localhost
  1356.         }
  1357.         return $l if -f $l && -r _;
  1358.         if (-f "$l.gz") {
  1359.         $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
  1360.         system("$CPAN::Config->{gzip} -dc $l.gz > $aslocal");
  1361.         return $aslocal if -f $aslocal;
  1362.         }
  1363.     }
  1364.  
  1365.     if ($CPAN::META->has_inst('LWP')) {
  1366.         print "Fetching $url with LWP\n";
  1367.         my $res = $Ua->mirror($url, $aslocal);
  1368.         if ($res->is_success) {
  1369.         return $aslocal;
  1370.         }
  1371.     }
  1372.     if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  1373.         my($host,$dir,$getfile) = ($1,$2,$3);
  1374.         if ($CPAN::META->has_inst('Net::FTP')) {
  1375.         $dir =~ s|/+|/|g;
  1376.         $self->debug("Going to fetch file [$getfile]
  1377.   from dir [$dir]
  1378.   on host  [$host]
  1379.   as local [$aslocal]") if $CPAN::DEBUG;
  1380.         CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal) && return $aslocal;
  1381.         warn "Net::FTP failed for some reason\n";
  1382.         }
  1383.     }
  1384.  
  1385.  
  1386.     my($funkyftp);
  1387.     for $funkyftp ($CPAN::Config->{'lynx'},$CPAN::Config->{'ncftp'}) {
  1388.         next unless defined $funkyftp;
  1389.         next if $funkyftp =~ /^\s*$/;
  1390.         my($want_compressed);
  1391.         print(
  1392.           qq{
  1393. Trying with $funkyftp to get
  1394.   $url
  1395. });
  1396.         $want_compressed = $aslocal =~ s/\.gz//;
  1397.         my($source_switch) = "";
  1398.         $source_switch = "-source" if $funkyftp =~ /\blynx$/;
  1399.         $source_switch = "-c" if $funkyftp =~ /\bncftp$/;
  1400.         my($system) = "$funkyftp $source_switch '$url' > $aslocal";
  1401.         $self->debug("system[$system]") if $CPAN::DEBUG;
  1402.         my($wstatus);
  1403.         if (($wstatus = system($system)) == 0
  1404.         &&
  1405.         -s $aslocal   # lynx returns 0 on my system even if it fails
  1406.            ) {
  1407.         if ($want_compressed) {
  1408.             $system = "$CPAN::Config->{'gzip'} -dt $aslocal";
  1409.             if (system($system) == 0) {
  1410.             rename $aslocal, "$aslocal.gz";
  1411.             } else {
  1412.             $system = "$CPAN::Config->{'gzip'} $aslocal";
  1413.             system($system);
  1414.             }
  1415.             return "$aslocal.gz";
  1416.         } else {
  1417.             $system = "$CPAN::Config->{'gzip'} -dt $aslocal";
  1418.             if (system($system) == 0) {
  1419.             $system = "$CPAN::Config->{'gzip'} -d $aslocal";
  1420.             system($system);
  1421.             } else {
  1422.             }
  1423.             return $aslocal;
  1424.         }
  1425.         } else {
  1426.         my $estatus = $wstatus >> 8;
  1427.         my $size = -s $aslocal;
  1428.         print qq{
  1429. System call "$system"
  1430. returned status $estatus (wstat $wstatus), left
  1431. $aslocal with size $size
  1432. };
  1433.         }
  1434.     }
  1435.  
  1436.     if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  1437.         my($host,$dir,$getfile) = ($1,$2,$3);
  1438.         my($netrcfile,$fh);
  1439.         if (-x $CPAN::Config->{'ftp'}) {
  1440.         my $timestamp = 0;
  1441.         my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
  1442.            $ctime,$blksize,$blocks) = stat($aslocal);
  1443.         $timestamp = $mtime ||= 0;
  1444.  
  1445.         my($netrc) = CPAN::FTP::netrc->new;
  1446.         my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
  1447.  
  1448.         my $targetfile = File::Basename::basename($aslocal);
  1449.         my(@dialog);
  1450.         push(
  1451.              @dialog,
  1452.              "lcd $aslocal_dir",
  1453.              "cd /",
  1454.              map("cd $_", split "/", $dir), # RFC 1738
  1455.              "bin",
  1456.              "get $getfile $targetfile",
  1457.              "quit"
  1458.             );
  1459.         if (! $netrc->netrc) {
  1460.             CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
  1461.         } elsif ($netrc->hasdefault || $netrc->contains($host)) {
  1462.             CPAN->debug(
  1463.                 sprint(
  1464.                        "hasdef[%d]cont($host)[%d]",
  1465.                        $netrc->hasdefault,
  1466.                        $netrc->contains($host)
  1467.                       )
  1468.                    ) if $CPAN::DEBUG;
  1469.             if ($netrc->protected) {
  1470.             print(
  1471.                   qq{
  1472.   Trying with external ftp to get
  1473.     $url
  1474.   As this requires some features that are not thoroughly tested, we\'re
  1475.   not sure, that we get it right....
  1476.  
  1477. }
  1478.                  );
  1479.             my $fh = FileHandle->new;
  1480.             $fh->open("|$CPAN::Config->{'ftp'}$verbose $host")
  1481.                 or die "Couldn't open ftp: $!";
  1482.             CPAN->debug("dialog [".(join "|",@dialog)."]")
  1483.                 if $CPAN::DEBUG;
  1484.             foreach (@dialog) { $fh->print("$_\n") }
  1485.             $fh->close;        # Wait for process to complete
  1486.             my $wstatus = $?;
  1487.             my $estatus = $wstatus >> 8;
  1488.             print qq{
  1489. Subprocess "|$CPAN::Config->{'ftp'}$verbose $host"
  1490.   returned status $estatus (wstat $wstatus)
  1491. } if $wstatus;
  1492.             ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  1493.              $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  1494.             $mtime ||= 0;
  1495.             if ($mtime > $timestamp) {
  1496.                 print "GOT $aslocal\n";
  1497.                 return $aslocal;
  1498.             } else {
  1499.                 print "Hmm... Still failed!\n";
  1500.             }
  1501.             } else {
  1502.             warn "Your $netrcfile is not correctly protected.\n";
  1503.             }
  1504.         } else {
  1505.             warn "Your ~/.netrc neither contains $host
  1506.   nor does it have a default entry\n";
  1507.         }
  1508.  
  1509.         print qq{Issuing "$CPAN::Config->{'ftp'}$verbose -n"\n};
  1510.         unshift(
  1511.             @dialog,
  1512.             "open $host",
  1513.             "user anonymous $Config::Config{'cf_email'}"
  1514.                );
  1515.         CPAN->debug("dialog [".(join "|",@dialog)."]") if $CPAN::DEBUG;
  1516.         $fh = FileHandle->new;
  1517.         $fh->open("|$CPAN::Config->{'ftp'}$verbose -n") or
  1518.             die "Cannot fork: $!\n";
  1519.         foreach (@dialog) { $fh->print("$_\n") }
  1520.         $fh->close;
  1521.         my $wstatus = $?;
  1522.         my $estatus = $wstatus >> 8;
  1523.         print qq{
  1524. Subprocess "|$CPAN::Config->{'ftp'}$verbose -n"
  1525.   returned status $estatus (wstat $wstatus)
  1526. } if $wstatus;
  1527.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  1528.            $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  1529.         $mtime ||= 0;
  1530.         if ($mtime > $timestamp) {
  1531.             print "GOT $aslocal\n";
  1532.             return $aslocal;
  1533.         } else {
  1534.             print "Bad luck... Still failed!\n";
  1535.         }
  1536.         }
  1537.         sleep 2;
  1538.     }
  1539.  
  1540.     print "Can't access URL $url.\n\n";
  1541.     my(@mess,$mess);
  1542.     push @mess, "LWP" unless CPAN->has_inst('LWP');
  1543.     push @mess, "Net::FTP" unless CPAN->has_inst('Net::FTP');
  1544.     my($ext);
  1545.     for $ext (qw/lynx ncftp ftp/) {
  1546.         $CPAN::Config->{$ext} ||= "";
  1547.         push @mess, "an external $ext" unless -x $CPAN::Config->{$ext};
  1548.     }
  1549.     $mess = qq{Either get }.
  1550.         join(" or ",@mess).
  1551.         qq{ or check, if the URL found in your configuration file, }.
  1552.         $CPAN::Config->{urllist}[$i].
  1553.         qq{, is valid.};
  1554.     print Text::Wrap::wrap("","",$mess), "\n";
  1555.     }
  1556.     print "Cannot fetch $file\n";
  1557.     if ($restore) {
  1558.     rename "$aslocal.bak", $aslocal;
  1559.     print "Trying to get away with old file:\n";
  1560.     print $self->ls($aslocal);
  1561.     return $aslocal;
  1562.     }
  1563.     return;
  1564. }
  1565.  
  1566. sub ls {
  1567.     my($self,$name) = @_;
  1568.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  1569.      $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
  1570.  
  1571.     my($perms,%user,%group);
  1572.     my $pname = $name;
  1573.  
  1574.     if ($blocks) {
  1575.     $blocks = int(($blocks + 1) / 2);
  1576.     }
  1577.     else {
  1578.     $blocks = int(($sizemm + 1023) / 1024);
  1579.     }
  1580.  
  1581.     if    (-f _) { $perms = '-'; }
  1582.     elsif (-d _) { $perms = 'd'; }
  1583.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  1584.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  1585.     elsif (-p _) { $perms = 'p'; }
  1586.     elsif (-S _) { $perms = 's'; }
  1587.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  1588.  
  1589.     my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  1590.     my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  1591.     my $tmpmode = $mode;
  1592.     my $tmp = $rwx[$tmpmode & 7];
  1593.     $tmpmode >>= 3;
  1594.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  1595.     $tmpmode >>= 3;
  1596.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  1597.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  1598.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  1599.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  1600.     $perms .= $tmp;
  1601.  
  1602.     my $user = $user{$uid} || $uid;   # too lazy to implement lookup
  1603.     my $group = $group{$gid} || $gid;
  1604.  
  1605.     my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  1606.     my($timeyear);
  1607.     my($moname) = $moname[$mon];
  1608.     if (-M _ > 365.25 / 2) {
  1609.     $timeyear = $year + 1900;
  1610.     }
  1611.     else {
  1612.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  1613.     }
  1614.  
  1615.     sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  1616.         $ino,
  1617.          $blocks,
  1618.               $perms,
  1619.                 $nlink,
  1620.                 $user,
  1621.                      $group,
  1622.                       $sizemm,
  1623.                           $moname,
  1624.                          $mday,
  1625.                              $timeyear,
  1626.                              $pname;
  1627. }
  1628.  
  1629. package CPAN::FTP::netrc;
  1630.  
  1631. sub new {
  1632.     my($class) = @_;
  1633.     my $file = MM->catfile($ENV{HOME},".netrc");
  1634.  
  1635.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  1636.        $atime,$mtime,$ctime,$blksize,$blocks)
  1637.     = stat($file);
  1638.     $mode ||= 0;
  1639.     my $protected = 0;
  1640.  
  1641.     my($fh,@machines,$hasdefault);
  1642.     $hasdefault = 0;
  1643.     $fh = FileHandle->new or die "Could not create a filehandle";
  1644.  
  1645.     if($fh->open($file)){
  1646.     $protected = ($mode & 077) == 0;
  1647.     local($/) = "";
  1648.       NETRC: while (<$fh>) {
  1649.         my(@tokens) = split " ", $_;
  1650.       TOKEN: while (@tokens) {
  1651.         my($t) = shift @tokens;
  1652.         if ($t eq "default"){
  1653.             $hasdefault++;
  1654.             last NETRC;
  1655.         }
  1656.         last TOKEN if $t eq "macdef";
  1657.         if ($t eq "machine") {
  1658.             push @machines, shift @tokens;
  1659.         }
  1660.         }
  1661.     }
  1662.     } else {
  1663.     $file = $hasdefault = $protected = "";
  1664.     }
  1665.  
  1666.     bless {
  1667.        'mach' => [@machines],
  1668.        'netrc' => $file,
  1669.        'hasdefault' => $hasdefault,
  1670.        'protected' => $protected,
  1671.       }, $class;
  1672. }
  1673.  
  1674. sub hasdefault { shift->{'hasdefault'} }
  1675. sub netrc      { shift->{'netrc'}      }
  1676. sub protected  { shift->{'protected'}  }
  1677. sub contains {
  1678.     my($self,$mach) = @_;
  1679.     for ( @{$self->{'mach'}} ) {
  1680.     return 1 if $_ eq $mach;
  1681.     }
  1682.     return 0;
  1683. }
  1684.  
  1685. package CPAN::Complete;
  1686.  
  1687. sub cpl {
  1688.     my($word,$line,$pos) = @_;
  1689.     $word ||= "";
  1690.     $line ||= "";
  1691.     $pos ||= 0;
  1692.     CPAN->debug("word [$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1693.     $line =~ s/^\s*//;
  1694.     if ($line =~ s/^(force\s*)//) {
  1695.     $pos -= length($1);
  1696.     }
  1697.     my @return;
  1698.     if ($pos == 0) {
  1699.     @return = grep(
  1700.                /^$word/,
  1701.                sort qw(
  1702.                    ! a b d h i m o q r u autobundle clean
  1703.                    make test install force reload look
  1704.                   )
  1705.               );
  1706.     } elsif ( $line !~ /^[\!abdhimorutl]/ ) {
  1707.     @return = ();
  1708.     } elsif ($line =~ /^a\s/) {
  1709.     @return = cplx('CPAN::Author',$word);
  1710.     } elsif ($line =~ /^b\s/) {
  1711.     @return = cplx('CPAN::Bundle',$word);
  1712.     } elsif ($line =~ /^d\s/) {
  1713.     @return = cplx('CPAN::Distribution',$word);
  1714.     } elsif ($line =~ /^([mru]|make|clean|test|install|readme|look)\s/ ) {
  1715.     @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  1716.     } elsif ($line =~ /^i\s/) {
  1717.     @return = cpl_any($word);
  1718.     } elsif ($line =~ /^reload\s/) {
  1719.     @return = cpl_reload($word,$line,$pos);
  1720.     } elsif ($line =~ /^o\s/) {
  1721.     @return = cpl_option($word,$line,$pos);
  1722.     } else {
  1723.     @return = ();
  1724.     }
  1725.     return @return;
  1726. }
  1727.  
  1728. sub cplx {
  1729.     my($class, $word) = @_;
  1730.     grep /^\Q$word\E/, map { $_->id } $CPAN::META->all($class);
  1731. }
  1732.  
  1733. sub cpl_any {
  1734.     my($word) = shift;
  1735.     return (
  1736.         cplx('CPAN::Author',$word),
  1737.         cplx('CPAN::Bundle',$word),
  1738.         cplx('CPAN::Distribution',$word),
  1739.         cplx('CPAN::Module',$word),
  1740.        );
  1741. }
  1742.  
  1743. sub cpl_reload {
  1744.     my($word,$line,$pos) = @_;
  1745.     $word ||= "";
  1746.     my(@words) = split " ", $line;
  1747.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1748.     my(@ok) = qw(cpan index);
  1749.     return @ok if @words == 1;
  1750.     return grep /^\Q$word\E/, @ok if @words == 2 && $word;
  1751. }
  1752.  
  1753. sub cpl_option {
  1754.     my($word,$line,$pos) = @_;
  1755.     $word ||= "";
  1756.     my(@words) = split " ", $line;
  1757.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1758.     my(@ok) = qw(conf debug);
  1759.     return @ok if @words == 1;
  1760.     return grep /^\Q$word\E/, @ok if @words == 2 && $word;
  1761.     if (0) {
  1762.     } elsif ($words[1] eq 'index') {
  1763.     return ();
  1764.     } elsif ($words[1] eq 'conf') {
  1765.     return CPAN::Config::cpl(@_);
  1766.     } elsif ($words[1] eq 'debug') {
  1767.     return sort grep /^\Q$word\E/, sort keys %CPAN::DEBUG, 'all';
  1768.     }
  1769. }
  1770.  
  1771. package CPAN::Index;
  1772.  
  1773. sub force_reload {
  1774.     my($class) = @_;
  1775.     $CPAN::Index::last_time = 0;
  1776.     $class->reload(1);
  1777. }
  1778.  
  1779. sub reload {
  1780.     my($cl,$force) = @_;
  1781.     my $time = time;
  1782.  
  1783.     for ($CPAN::Config->{index_expire}) {
  1784.     $_ = 0.001 unless $_ > 0.001;
  1785.     }
  1786.     return if $last_time + $CPAN::Config->{index_expire}*86400 > $time;
  1787.     my($debug,$t2);
  1788.     $last_time = $time;
  1789.  
  1790.     $cl->rd_authindex($cl->reload_x(
  1791.                       "authors/01mailrc.txt.gz",
  1792.                       "01mailrc.gz",
  1793.                       $force));
  1794.     $t2 = time;
  1795.     $debug = "timing reading 01[".($t2 - $time)."]";
  1796.     $time = $t2;
  1797.     return if $CPAN::Signal; # this is sometimes lengthy
  1798.     $cl->rd_modpacks($cl->reload_x(
  1799.                      "modules/02packages.details.txt.gz",
  1800.                      "02packag.gz",
  1801.                      $force));
  1802.     $t2 = time;
  1803.     $debug .= "02[".($t2 - $time)."]";
  1804.     $time = $t2;
  1805.     return if $CPAN::Signal; # this is sometimes lengthy
  1806.     $cl->rd_modlist($cl->reload_x(
  1807.                     "modules/03modlist.data.gz",
  1808.                     "03mlist.gz",
  1809.                     $force));
  1810.     $t2 = time;
  1811.     $debug .= "03[".($t2 - $time)."]";
  1812.     $time = $t2;
  1813.     CPAN->debug($debug) if $CPAN::DEBUG;
  1814. }
  1815.  
  1816. sub reload_x {
  1817.     my($cl,$wanted,$localname,$force) = @_;
  1818.     $force ||= 0;
  1819.     CPAN::Config->load; # we should guarantee loading wherever we rely
  1820.     my $abs_wanted = CPAN->catfile($CPAN::Config->{'keep_source_where'},
  1821.                    $localname);
  1822.     if (
  1823.     -f $abs_wanted &&
  1824.     -M $abs_wanted < $CPAN::Config->{'index_expire'} &&
  1825.     !$force
  1826.        ) {
  1827.     my $s = $CPAN::Config->{'index_expire'} == 1 ? "" : "s";
  1828.     $cl->debug(qq{$abs_wanted younger than $CPAN::Config->{'index_expire'} }.
  1829.            qq{day$s. I\'ll use that.});
  1830.     return $abs_wanted;
  1831.     } else {
  1832.     $force ||= 1;
  1833.     }
  1834.     return CPAN::FTP->localize($wanted,$abs_wanted,$force);
  1835. }
  1836.  
  1837. sub rd_authindex {
  1838.     my($cl,$index_target) = @_;
  1839.     my $pipe = "$CPAN::Config->{gzip} --decompress --stdout $index_target";
  1840.     print "Going to read $index_target\n";
  1841.     my $fh = FileHandle->new("$pipe|");
  1842.     while (<$fh>) {
  1843.     chomp;
  1844.     my($userid,$fullname,$email) = /alias\s+(\S+)\s+\"([^\"\<]+)\s+<([^\>]+)\>\"/;
  1845.     next unless $userid && $fullname && $email;
  1846.  
  1847.      my $userobj = $CPAN::META->instance('CPAN::Author',$userid);
  1848.     $userobj->set('FULLNAME' => $fullname, 'EMAIL' => $email);
  1849.     return if $CPAN::Signal;
  1850.     }
  1851.     $fh->close;
  1852.     $? and Carp::croak "FAILED $pipe: exit status [$?]";
  1853. }
  1854.  
  1855. sub rd_modpacks {
  1856.     my($cl,$index_target) = @_;
  1857.     my $pipe = "$CPAN::Config->{gzip} --decompress --stdout $index_target";
  1858.     print "Going to read $index_target\n";
  1859.     my $fh = FileHandle->new("$pipe|");
  1860.     while (<$fh>) {
  1861.     last if /^\s*$/;
  1862.     }
  1863.     while (<$fh>) {
  1864.     chomp;
  1865.     my($mod,$version,$dist) = split;
  1866.  
  1867.     my($bundle,$id,$userid);
  1868.     
  1869.     if ($mod eq 'CPAN') {
  1870.         local($^W)= 0;
  1871.         if ($version > $CPAN::VERSION){
  1872.         print qq{
  1873.   There\'s a new CPAN.pm version (v$version) available!
  1874.   You might want to try
  1875.     install CPAN
  1876.     reload cpan
  1877.   without quitting the current session. It should be a seemless upgrade
  1878.   while we are running...
  1879. };
  1880.         sleep 2;
  1881.         print qq{\n};
  1882.         }
  1883.         last if $CPAN::Signal;
  1884.     } elsif ($mod =~ /^Bundle::(.*)/) {
  1885.         $bundle = $1;
  1886.     }
  1887.  
  1888.     if ($bundle){
  1889.         $id =  $CPAN::META->instance('CPAN::Bundle',$mod);
  1890.     } else {
  1891.         $id = $CPAN::META->instance('CPAN::Module',$mod);
  1892.     }
  1893.  
  1894.     if ($id->cpan_file ne $dist){
  1895.         ($userid) = $dist =~ /([^\/]+)/;
  1896.         $id->set(
  1897.              'CPAN_USERID' => $userid,
  1898.              'CPAN_VERSION' => $version,
  1899.              'CPAN_FILE' => $dist
  1900.             );
  1901.     }
  1902.  
  1903.     unless ($CPAN::META->exists('CPAN::Distribution',$dist)) {
  1904.         $CPAN::META->instance(
  1905.                   'CPAN::Distribution' => $dist
  1906.                  )->set(
  1907.                     'CPAN_USERID' => $userid
  1908.                        );
  1909.     }
  1910.  
  1911.     return if $CPAN::Signal;
  1912.     }
  1913.     $fh->close;
  1914.     $? and Carp::croak "FAILED $pipe: exit status [$?]";
  1915. }
  1916.  
  1917. sub rd_modlist {
  1918.     my($cl,$index_target) = @_;
  1919.     my $pipe = "$CPAN::Config->{gzip} --decompress --stdout $index_target";
  1920.     print "Going to read $index_target\n";
  1921.     my $fh = FileHandle->new("$pipe|");
  1922.     my $eval;
  1923.     while (<$fh>) {
  1924.     if (/^Date:\s+(.*)/){
  1925.         return if $date_of_03 eq $1;
  1926.         ($date_of_03) = $1;
  1927.     }
  1928.     last if /^\s*$/;
  1929.     }
  1930.     local($/) = undef;
  1931.     $eval = <$fh>;
  1932.     $fh->close;
  1933.     $eval .= q{CPAN::Modulelist->data;};
  1934.     local($^W) = 0;
  1935.     my($comp) = Safe->new("CPAN::Safe1");
  1936.     my $ret = $comp->reval($eval);
  1937.     Carp::confess($@) if $@;
  1938.     return if $CPAN::Signal;
  1939.     for (keys %$ret) {
  1940.     my $obj = $CPAN::META->instance(CPAN::Module,$_);
  1941.     $obj->set(%{$ret->{$_}});
  1942.     return if $CPAN::Signal;
  1943.     }
  1944. }
  1945.  
  1946. package CPAN::InfoObj;
  1947.  
  1948. sub new { my $this = bless {}, shift; %$this = @_; $this }
  1949.  
  1950. sub set {
  1951.     my($self,%att) = @_;
  1952.     my(%oldatt) = %$self;
  1953.     %$self = (%oldatt, %att);
  1954. }
  1955.  
  1956. sub id { shift->{'ID'} }
  1957.  
  1958. sub as_glimpse {
  1959.     my($self) = @_;
  1960.     my(@m);
  1961.     my $class = ref($self);
  1962.     $class =~ s/^CPAN:://;
  1963.     push @m, sprintf "%-15s %s\n", $class, $self->{ID};
  1964.     join "", @m;
  1965. }
  1966.  
  1967. sub as_string {
  1968.     my($self) = @_;
  1969.     my(@m);
  1970.     my $class = ref($self);
  1971.     $class =~ s/^CPAN:://;
  1972.     push @m, $class, " id = $self->{ID}\n";
  1973.     for (sort keys %$self) {
  1974.     next if $_ eq 'ID';
  1975.     my $extra = "";
  1976.     $_ eq "CPAN_USERID" and $extra = " (".$self->author.")";
  1977.     if (ref($self->{$_}) eq "ARRAY") { # Should we setup a language interface? XXX
  1978.         push @m, sprintf "    %-12s %s%s\n", $_, "@{$self->{$_}}", $extra;
  1979.     } else {
  1980.         push @m, sprintf "    %-12s %s%s\n", $_, $self->{$_}, $extra;
  1981.     }
  1982.     }
  1983.     join "", @m, "\n";
  1984. }
  1985.  
  1986. sub author {
  1987.     my($self) = @_;
  1988.     $CPAN::META->instance(CPAN::Author,$self->{CPAN_USERID})->fullname;
  1989. }
  1990.  
  1991. package CPAN::Author;
  1992.  
  1993. sub as_glimpse {
  1994.     my($self) = @_;
  1995.     my(@m);
  1996.     my $class = ref($self);
  1997.     $class =~ s/^CPAN:://;
  1998.     push @m, sprintf "%-15s %s (%s)\n", $class, $self->{ID}, $self->fullname;
  1999.     join "", @m;
  2000. }
  2001.  
  2002.  
  2003. sub fullname { shift->{'FULLNAME'} }
  2004. *name = \&fullname;
  2005. sub email    { shift->{'EMAIL'} }
  2006.  
  2007. package CPAN::Distribution;
  2008.  
  2009. sub called_for {
  2010.     my($self,$id) = @_;
  2011.     $self->{'CALLED_FOR'} = $id if defined $id;
  2012.     return $self->{'CALLED_FOR'};
  2013. }
  2014.  
  2015. sub get {
  2016.     my($self) = @_;
  2017.   EXCUSE: {
  2018.     my @e;
  2019.     exists $self->{'build_dir'} and push @e,
  2020.         "Unwrapped into directory $self->{'build_dir'}";
  2021.     print join "", map {"  $_\n"} @e and return if @e;
  2022.     }
  2023.     my($local_file);
  2024.     my($local_wanted) =
  2025.      CPAN->catfile(
  2026.             $CPAN::Config->{keep_source_where},
  2027.             "authors",
  2028.             "id",
  2029.             split("/",$self->{ID})
  2030.                );
  2031.  
  2032.     $self->debug("Doing localize") if $CPAN::DEBUG;
  2033.     $local_file = CPAN::FTP->localize("authors/id/$self->{ID}", $local_wanted);
  2034.     $self->{localfile} = $local_file;
  2035.     my $builddir = $CPAN::META->{cachemgr}->dir;
  2036.     $self->debug("doing chdir $builddir") if $CPAN::DEBUG;
  2037.     chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  2038.     my $packagedir;
  2039.  
  2040.     $self->debug("local_file[$local_file]") if $CPAN::DEBUG;
  2041.     if ($CPAN::META->has_inst('MD5')) {
  2042.     $self->debug("MD5 is installed, verifying");
  2043.     $self->verifyMD5;
  2044.     } else {
  2045.     $self->debug("MD5 is NOT installed");
  2046.     }
  2047.     $self->debug("Removing tmp") if $CPAN::DEBUG;
  2048.     File::Path::rmtree("tmp");
  2049.     mkdir "tmp", 0755 or Carp::croak "Couldn't mkdir tmp: $!";
  2050.     chdir "tmp";
  2051.     $self->debug("Changed directory to tmp") if $CPAN::DEBUG;
  2052.     if ($local_file =~ /(\.tar\.(gz|Z)|\.tgz)$/i){
  2053.     $self->untar_me($local_file);
  2054.     } elsif ( $local_file =~ /\.zip$/i ) {
  2055.     $self->unzip_me($local_file);
  2056.     } elsif ( $local_file =~ /\.pm\.(gz|Z)$/) {
  2057.     $self->pm2dir_me($local_file);
  2058.     } else {
  2059.     $self->{archived} = "NO";
  2060.     }
  2061.     chdir "..";
  2062.     if ($self->{archived} ne 'NO') {
  2063.     chdir "tmp";
  2064.     my $dh = DirHandle->new(".") or Carp::croak("Couldn't opendir .: $!");
  2065.     my @readdir = grep $_ !~ /^\.\.?$/, $dh->read; ### MAC??
  2066.     $dh->close;
  2067.     my ($distdir,$packagedir);
  2068.     if (@readdir == 1 && -d $readdir[0]) {
  2069.         $distdir = $readdir[0];
  2070.         $packagedir = $CPAN::META->catdir($builddir,$distdir);
  2071.         -d $packagedir and print "Removing previously used $packagedir\n";
  2072.         File::Path::rmtree($packagedir);
  2073.         rename($distdir,$packagedir) or Carp::confess("Couldn't rename $distdir to $packagedir: $!");
  2074.     } else {
  2075.         my $pragmatic_dir = $self->{'CPAN_USERID'} . '000';
  2076.         $pragmatic_dir =~ s/\W_//g;
  2077.         $pragmatic_dir++ while -d "../$pragmatic_dir";
  2078.         $packagedir = $CPAN::META->catdir($builddir,$pragmatic_dir);
  2079.         File::Path::mkpath($packagedir);
  2080.         my($f);
  2081.         for $f (@readdir) { # is already without "." and ".."
  2082.         my $to = $CPAN::META->catdir($packagedir,$f);
  2083.         rename($f,$to) or Carp::confess("Couldn't rename $f to $to: $!");
  2084.         }
  2085.     }
  2086.     $self->{'build_dir'} = $packagedir;
  2087.     chdir "..";
  2088.  
  2089.     $self->debug("Changed directory to .. (self is $self [".$self->as_string."])")
  2090.         if $CPAN::DEBUG;
  2091.     File::Path::rmtree("tmp");
  2092.     if ($CPAN::Config->{keep_source_where} =~ /^no/i ){
  2093.         print "Going to unlink $local_file\n";
  2094.         unlink $local_file or Carp::carp "Couldn't unlink $local_file";
  2095.     }
  2096.     my($makefilepl) = $CPAN::META->catfile($packagedir,"Makefile.PL");
  2097.     unless (-f $makefilepl) {
  2098.         my($configure) = $CPAN::META->catfile($packagedir,"Configure");
  2099.         if (-f $configure) {
  2100.         $self->{'configure'} = $configure;
  2101.         } else {
  2102.         my $fh = FileHandle->new(">$makefilepl")
  2103.             or Carp::croak("Could not open >$makefilepl");
  2104.         my $cf = $self->called_for || "unknown";
  2105.         $fh->print(
  2106. qq{# This Makefile.PL has been autogenerated by the module CPAN.pm
  2107.  
  2108.             use ExtUtils::MakeMaker;
  2109.             WriteMakefile(NAME => q[$cf]);
  2110.  
  2111. });
  2112.         print qq{Package comes without Makefile.PL.\n}.
  2113.             qq{  Writing one on our own (calling it $cf)\n};
  2114.         }
  2115.     }
  2116.     }
  2117.     return $self;
  2118. }
  2119.  
  2120. sub untar_me {
  2121.     my($self,$local_file) = @_;
  2122.     $self->{archived} = "tar";
  2123.     my $system = "$CPAN::Config->{gzip} --decompress --stdout " .
  2124.     "$local_file | $CPAN::Config->{tar} xvf -";
  2125.     if (system($system)== 0) {
  2126.     $self->{unwrapped} = "YES";
  2127.     } else {
  2128.     $self->{unwrapped} = "NO";
  2129.     }
  2130. }
  2131.  
  2132. sub unzip_me {
  2133.     my($self,$local_file) = @_;
  2134.     $self->{archived} = "zip";
  2135.     my $system = "$CPAN::Config->{unzip} $local_file";
  2136.     if (system($system) == 0) {
  2137.     $self->{unwrapped} = "YES";
  2138.     } else {
  2139.     $self->{unwrapped} = "NO";
  2140.     }
  2141. }
  2142.  
  2143. sub pm2dir_me {
  2144.     my($self,$local_file) = @_;
  2145.     $self->{archived} = "pm";
  2146.     my $to = File::Basename::basename($local_file);
  2147.     $to =~ s/\.(gz|Z)$//;
  2148.     my $system = "$CPAN::Config->{gzip} --decompress --stdout $local_file > $to";
  2149.     if (system($system) == 0) {
  2150.     $self->{unwrapped} = "YES";
  2151.     } else {
  2152.     $self->{unwrapped} = "NO";
  2153.     }
  2154. }
  2155.  
  2156. sub new {
  2157.     my($class,%att) = @_;
  2158.  
  2159.     $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new();
  2160.  
  2161.     my $this = { %att };
  2162.     return bless $this, $class;
  2163. }
  2164.  
  2165. sub look {
  2166.     my($self) = @_;
  2167.     if (  $CPAN::Config->{'shell'} ) {
  2168.     print qq{
  2169. Trying to open a subshell in the build directory...
  2170. };
  2171.     } else {
  2172.     print qq{
  2173. Your configuration does not define a value for subshells.
  2174. Please define it with "o conf shell <your shell>"
  2175. };
  2176.     return;
  2177.     }
  2178.     my $dist = $self->id;
  2179.     my $dir  = $self->dir or $self->get;
  2180.     $dir = $self->dir;
  2181.     my $getcwd;
  2182.     $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  2183.     my $pwd  = CPAN->$getcwd();
  2184.     chdir($dir);
  2185.     print qq{Working directory is $dir.\n};
  2186.     system($CPAN::Config->{'shell'}) == 0 or die "Subprocess shell error";
  2187.     chdir($pwd);
  2188. }
  2189.  
  2190. sub readme {
  2191.     my($self) = @_;
  2192.     my($dist) = $self->id;
  2193.     my($sans,$suffix) = $dist =~ /(.+)\.(tgz|tar[\._-]gz|tar\.Z|zip)$/;
  2194.     $self->debug("sans[$sans] suffix[$suffix]\n") if $CPAN::DEBUG;
  2195.     my($local_file);
  2196.     my($local_wanted) =
  2197.      CPAN->catfile(
  2198.             $CPAN::Config->{keep_source_where},
  2199.             "authors",
  2200.             "id",
  2201.             split("/","$sans.readme"),
  2202.                );
  2203.     $self->debug("Doing localize") if $CPAN::DEBUG;
  2204.     $local_file = CPAN::FTP->localize("authors/id/$sans.readme", $local_wanted);
  2205.     my $fh_pager = FileHandle->new;
  2206.     $fh_pager->open("|$CPAN::Config->{'pager'}")
  2207.     or die "Could not open pager $CPAN::Config->{'pager'}: $!";
  2208.     my $fh_readme = FileHandle->new;
  2209.     $fh_readme->open($local_file) or die "Could not open $local_file: $!";
  2210.     $fh_pager->print(<$fh_readme>);
  2211. }
  2212.  
  2213. sub verifyMD5 {
  2214.     my($self) = @_;
  2215.   EXCUSE: {
  2216.     my @e;
  2217.     $self->{MD5_STATUS} ||= "";
  2218.     $self->{MD5_STATUS} eq "OK" and push @e, "MD5 Checksum was ok";
  2219.     print join "", map {"  $_\n"} @e and return if @e;
  2220.     }
  2221.     my($lc_want,$lc_file,@local,$basename);
  2222.     @local = split("/",$self->{ID});
  2223.     pop @local;
  2224.     push @local, "CHECKSUMS";
  2225.     $lc_want =
  2226.     CPAN->catfile($CPAN::Config->{keep_source_where},
  2227.               "authors", "id", @local);
  2228.     local($") = "/";
  2229.     if (
  2230.     -f $lc_want
  2231.     &&
  2232.     $self->MD5_check_file($lc_want)
  2233.        ) {
  2234.     return $self->{MD5_STATUS} = "OK";
  2235.     }
  2236.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  2237.                    $lc_want,'force>:-{');
  2238.     unless ($lc_file) {
  2239.     $local[-1] .= ".gz";
  2240.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  2241.                        "$lc_want.gz",'force>:-{');
  2242.     my @system = ($CPAN::Config->{gzip}, '--decompress', $lc_file);
  2243.     system(@system) == 0 or die "Could not uncompress $lc_file";
  2244.     $lc_file =~ s/\.gz$//;
  2245.     }
  2246.     $self->MD5_check_file($lc_file);
  2247. }
  2248.  
  2249. sub MD5_check_file {
  2250.     my($self,$chk_file) = @_;
  2251.     my($cksum,$file,$basename);
  2252.     $file =  $self->{localfile};
  2253.     $basename = File::Basename::basename($file);
  2254.     my $fh = FileHandle->new;
  2255.     local($/);
  2256.     if (open $fh, $chk_file){
  2257.     my $eval = <$fh>;
  2258.     close $fh;
  2259.     my($comp) = Safe->new();
  2260.     $cksum = $comp->reval($eval);
  2261.     if ($@) {
  2262.         rename $chk_file, "$chk_file.bad";
  2263.         Carp::confess($@) if $@;
  2264.     }
  2265.     } else {
  2266.     Carp::carp "Could not open $chk_file for reading";
  2267.     }
  2268.     if ($cksum->{$basename}->{md5}) {
  2269.     $self->debug("Found checksum for $basename:" .
  2270.              "$cksum->{$basename}->{md5}\n") if $CPAN::DEBUG;
  2271.     my $pipe = "$CPAN::Config->{gzip} --decompress ".
  2272.         "--stdout $file|";
  2273.     if (
  2274.         open($fh, $file) &&
  2275.         binmode $fh &&
  2276.         $self->eq_MD5($fh,$cksum->{$basename}->{md5})
  2277.         or
  2278.         open($fh, $pipe) &&
  2279.         binmode $fh  &&
  2280.         $self->eq_MD5($fh,$cksum->{$basename}->{'md5-ungz'})
  2281.        ){
  2282.         print "Checksum for $file ok\n";
  2283.         return $self->{MD5_STATUS} = "OK";
  2284.     } else {
  2285.         print qq{Checksum mismatch for distribution file. }.
  2286.         qq{Please investigate.\n\n};
  2287.         print $self->as_string;
  2288.         print $CPAN::META->instance(
  2289.                     'CPAN::Author',
  2290.                     $self->{CPAN_USERID}
  2291.                        )->as_string;
  2292.         my $wrap = qq{I\'d recommend removing $file. It seems to
  2293. be a bogus file.  Maybe you have configured your \`urllist\' with a
  2294. bad URL.  Please check this array with \`o conf urllist\', and
  2295. retry.};
  2296.         print Text::Wrap::wrap("","",$wrap);
  2297.         print "\n\n";
  2298.         sleep 3;
  2299.         return;
  2300.     }
  2301.     close $fh if fileno($fh);
  2302.     } else {
  2303.     $self->{MD5_STATUS} ||= "";
  2304.     if ($self->{MD5_STATUS} eq "NIL") {
  2305.         print "\nNo md5 checksum for $basename in local $chk_file.";
  2306.         print "Removing $chk_file\n";
  2307.         unlink $chk_file or print "Could not unlink: $!";
  2308.         sleep 1;
  2309.     }
  2310.     $self->{MD5_STATUS} = "NIL";
  2311.     return;
  2312.     }
  2313. }
  2314.  
  2315. sub eq_MD5 {
  2316.     my($self,$fh,$expectMD5) = @_;
  2317.     my $md5 = MD5->new;
  2318.     $md5->addfile($fh);
  2319.     my $hexdigest = $md5->hexdigest;
  2320.     $hexdigest eq $expectMD5;
  2321. }
  2322.  
  2323. sub force {
  2324.     my($self) = @_;
  2325.     $self->{'force_update'}++;
  2326.     delete $self->{'MD5_STATUS'};
  2327.     delete $self->{'archived'};
  2328.     delete $self->{'build_dir'};
  2329.     delete $self->{'localfile'};
  2330.     delete $self->{'make'};
  2331.     delete $self->{'install'};
  2332.     delete $self->{'unwrapped'};
  2333.     delete $self->{'writemakefile'};
  2334. }
  2335.  
  2336. sub perl {
  2337.     my($self) = @_;
  2338.     my($perl) = MM->file_name_is_absolute($^X) ? $^X : "";
  2339.     my $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  2340.     my $pwd  = CPAN->$getcwd();
  2341.     my $candidate = $CPAN::META->catfile($pwd,$^X);
  2342.     $perl ||= $candidate if MM->maybe_command($candidate);
  2343.     unless ($perl) {
  2344.     my ($component,$perl_name);
  2345.       DIST_PERLNAME: foreach $perl_name ($^X, 'perl', 'perl5', "perl$]") {
  2346.         PATH_COMPONENT: foreach $component (MM->path(), $Config::Config{'binexp'}) {
  2347.           next unless defined($component) && $component;
  2348.           my($abs) = MM->catfile($component,$perl_name);
  2349.           if (MM->maybe_command($abs)) {
  2350.               $perl = $abs;
  2351.               last DIST_PERLNAME;
  2352.           }
  2353.           }
  2354.       }
  2355.     }
  2356.     $perl;
  2357. }
  2358.  
  2359. sub make {
  2360.     my($self) = @_;
  2361.     $self->debug($self->id) if $CPAN::DEBUG;
  2362.     print "Running make\n";
  2363.     $self->get;
  2364.   EXCUSE: {
  2365.     my @e;
  2366.     $self->{archived} eq "NO" and push @e,
  2367.     "Is neither a tar nor a zip archive.";
  2368.  
  2369.     $self->{unwrapped} eq "NO" and push @e,
  2370.     "had problems unarchiving. Please build manually";
  2371.  
  2372.     exists $self->{writemakefile} &&
  2373.         $self->{writemakefile} eq "NO" and push @e,
  2374.         "Had some problem writing Makefile";
  2375.  
  2376.     defined $self->{'make'} and push @e,
  2377.     "Has already been processed within this session";
  2378.  
  2379.     print join "", map {"  $_\n"} @e and return if @e;
  2380.     }
  2381.     print "\n  CPAN.pm: Going to build ".$self->id."\n\n";
  2382.     my $builddir = $self->dir;
  2383.     chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  2384.     $self->debug("Changed directory to $builddir") if $CPAN::DEBUG;
  2385.  
  2386.     my $system;
  2387.     if ($self->{'configure'}) {
  2388.     $system = $self->{'configure'};
  2389.     } else {
  2390.     my($perl) = $self->perl or die "Couldn\'t find executable perl\n";
  2391.     my $switch = "";
  2392.     $system = "$perl $switch Makefile.PL $CPAN::Config->{makepl_arg}";
  2393.     }
  2394.     {
  2395.     local($SIG{ALRM}) = sub { die "inactivity_timeout reached\n" };
  2396.     my($ret,$pid);
  2397.     $@ = "";
  2398.     if ($CPAN::Config->{inactivity_timeout}) {
  2399.         eval {
  2400.         alarm $CPAN::Config->{inactivity_timeout};
  2401.         local $SIG{CHLD} = sub { wait };
  2402.         if (defined($pid = fork)) {
  2403.             if ($pid) { #parent
  2404.             wait;
  2405.             } else {    #child
  2406.             exec $system;
  2407.             }
  2408.         } else {
  2409.             print "Cannot fork: $!";
  2410.             return;
  2411.         }
  2412.         };
  2413.         alarm 0;
  2414.         if ($@){
  2415.         kill 9, $pid;
  2416.         waitpid $pid, 0;
  2417.         print $@;
  2418.         $self->{writemakefile} = "NO - $@";
  2419.         $@ = "";
  2420.         return;
  2421.         }
  2422.     } else {
  2423.         $ret = system($system);
  2424.         if ($ret != 0) {
  2425.         $self->{writemakefile} = "NO";
  2426.         return;
  2427.         }
  2428.     }
  2429.     }
  2430.     $self->{writemakefile} = "YES";
  2431.     return if $CPAN::Signal;
  2432.     $system = join " ", $CPAN::Config->{'make'}, $CPAN::Config->{make_arg};
  2433.     if (system($system) == 0) {
  2434.      print "  $system -- OK\n";
  2435.      $self->{'make'} = "YES";
  2436.     } else {
  2437.      $self->{writemakefile} = "YES";
  2438.      $self->{'make'} = "NO";
  2439.      print "  $system -- NOT OK\n";
  2440.     }
  2441. }
  2442.  
  2443. sub test {
  2444.     my($self) = @_;
  2445.     $self->make;
  2446.     return if $CPAN::Signal;
  2447.     print "Running make test\n";
  2448.   EXCUSE: {
  2449.     my @e;
  2450.     exists $self->{'make'} or push @e,
  2451.     "Make had some problems, maybe interrupted? Won't test";
  2452.  
  2453.     exists $self->{'make'} and
  2454.         $self->{'make'} eq 'NO' and
  2455.         push @e, "Oops, make had returned bad status";
  2456.  
  2457.     exists $self->{'build_dir'} or push @e, "Has no own directory";
  2458.     print join "", map {"  $_\n"} @e and return if @e;
  2459.     }
  2460.     chdir $self->{'build_dir'} or Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  2461.     $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  2462.     my $system = join " ", $CPAN::Config->{'make'}, "test";
  2463.     if (system($system) == 0) {
  2464.      print "  $system -- OK\n";
  2465.      $self->{'make_test'} = "YES";
  2466.     } else {
  2467.      $self->{'make_test'} = "NO";
  2468.      print "  $system -- NOT OK\n";
  2469.     }
  2470. }
  2471.  
  2472. sub clean {
  2473.     my($self) = @_;
  2474.     print "Running make clean\n";
  2475.   EXCUSE: {
  2476.     my @e;
  2477.     exists $self->{'build_dir'} or push @e, "Has no own directory";
  2478.     print join "", map {"  $_\n"} @e and return if @e;
  2479.     }
  2480.     chdir $self->{'build_dir'} or Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  2481.     $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  2482.     my $system = join " ", $CPAN::Config->{'make'}, "clean";
  2483.     if (system($system) == 0) {
  2484.     print "  $system -- OK\n";
  2485.     $self->force;
  2486.     } else {
  2487.     }
  2488. }
  2489.  
  2490. sub install {
  2491.     my($self) = @_;
  2492.     $self->test;
  2493.     return if $CPAN::Signal;
  2494.     print "Running make install\n";
  2495.   EXCUSE: {
  2496.     my @e;
  2497.     exists $self->{'build_dir'} or push @e, "Has no own directory";
  2498.  
  2499.     exists $self->{'make'} or push @e,
  2500.     "Make had some problems, maybe interrupted? Won't install";
  2501.  
  2502.     exists $self->{'make'} and
  2503.         $self->{'make'} eq 'NO' and
  2504.         push @e, "Oops, make had returned bad status";
  2505.  
  2506.     push @e, "make test had returned bad status, won't install without force"
  2507.         if exists $self->{'make_test'} and
  2508.         $self->{'make_test'} eq 'NO' and
  2509.         ! $self->{'force_update'};
  2510.  
  2511.     exists $self->{'install'} and push @e,
  2512.     $self->{'install'} eq "YES" ?
  2513.         "Already done" : "Already tried without success";
  2514.  
  2515.     print join "", map {"  $_\n"} @e and return if @e;
  2516.     }
  2517.     chdir $self->{'build_dir'} or Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  2518.     $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  2519.     my $system = join " ", $CPAN::Config->{'make'}, "install", $CPAN::Config->{make_install_arg};
  2520.     my($pipe) = FileHandle->new("$system 2>&1 |");
  2521.     my($makeout) = "";
  2522.     while (<$pipe>){
  2523.     print;
  2524.     $makeout .= $_;
  2525.     }
  2526.     $pipe->close;
  2527.     if ($?==0) {
  2528.      print "  $system -- OK\n";
  2529.      $self->{'install'} = "YES";
  2530.     } else {
  2531.      $self->{'install'} = "NO";
  2532.      print "  $system -- NOT OK\n";
  2533.      if ($makeout =~ /permission/s && $> > 0) {
  2534.          print "    You may have to su to root to install the package\n";
  2535.      }
  2536.     }
  2537. }
  2538.  
  2539. sub dir {
  2540.     shift->{'build_dir'};
  2541. }
  2542.  
  2543. package CPAN::Bundle;
  2544.  
  2545. sub as_string {
  2546.     my($self) = @_;
  2547.     $self->contains;
  2548.     $self->{INST_VERSION} = $self->inst_version;
  2549.     return $self->SUPER::as_string;
  2550. }
  2551.  
  2552. sub contains {
  2553.     my($self) = @_;
  2554.     my($parsefile) = $self->inst_file;
  2555.     unless ($parsefile) {
  2556.     $self->debug("no parsefile") if $CPAN::DEBUG;
  2557.     my $dist = $CPAN::META->instance('CPAN::Distribution',$self->{'CPAN_FILE'});
  2558.     $dist->get;
  2559.     $self->debug($dist->as_string) if $CPAN::DEBUG;
  2560.     my($todir) = $CPAN::META->catdir($CPAN::Config->{'cpan_home'},"Bundle");
  2561.     File::Path::mkpath($todir);
  2562.     my($me,$from,$to);
  2563.     ($me = $self->id) =~ s/.*://;
  2564.     $from = $self->find_bundle_file($dist->{'build_dir'},"$me.pm");
  2565.     $to = $CPAN::META->catfile($todir,"$me.pm");
  2566.     File::Copy::copy($from, $to) or Carp::confess("Couldn't copy $from to $to: $!");
  2567.     $parsefile = $to;
  2568.     }
  2569.     my @result;
  2570.     my $fh = FileHandle->new;
  2571.     local $/ = "\n";
  2572.     open($fh,$parsefile) or die "Could not open '$parsefile': $!";
  2573.     my $inpod = 0;
  2574.     $self->debug("parsefile[$parsefile]") if $CPAN::DEBUG;
  2575.     while (<$fh>) {
  2576.     $inpod = /^=(?!head1\s+CONTENTS)/ ? 0 : /^=head1\s+CONTENTS/ ? 1 : $inpod;
  2577.     next unless $inpod;
  2578.     next if /^=/;
  2579.     next if /^\s+$/;
  2580.     chomp;
  2581.     push @result, (split " ", $_, 2)[0];
  2582.     }
  2583.     close $fh;
  2584.     delete $self->{STATUS};
  2585.     $self->{CONTAINS} = join ", ", @result;
  2586.     $self->debug("CONTAINS[@result]") if $CPAN::DEBUG;
  2587.     @result;
  2588. }
  2589.  
  2590. sub find_bundle_file {
  2591.     my($self,$where,$what) = @_;
  2592.     my $bu = $CPAN::META->catfile($where,$what);
  2593.     return $bu if -f $bu;
  2594.     my $manifest = $CPAN::META->catfile($where,"MANIFEST");
  2595.     unless (-f $manifest) {
  2596.     require ExtUtils::Manifest;
  2597.     my $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  2598.     my $cwd = CPAN->$getcwd();
  2599.     chdir $where;
  2600.     ExtUtils::Manifest::mkmanifest();
  2601.     chdir $cwd;
  2602.     }
  2603.     my $fh = FileHandle->new($manifest) or Carp::croak("Couldn't open $manifest: $!");
  2604.     local($/) = "\n";
  2605.     while (<$fh>) {
  2606.     next if /^\s*\#/;
  2607.     my($file) = /(\S+)/;
  2608.     if ($file =~ m|Bundle/$what$|) {
  2609.         $bu = $file;
  2610.         return $CPAN::META->catfile($where,$bu);
  2611.     }
  2612.     }
  2613.     Carp::croak("Could't find a Bundle file in $where");
  2614. }
  2615.  
  2616. sub inst_file {
  2617.     my($self) = @_;
  2618.     my($me,$inst_file);
  2619.     ($me = $self->id) =~ s/.*://;
  2620.     $inst_file = $CPAN::META->catfile($CPAN::Config->{'cpan_home'},"Bundle", "$me.pm");
  2621.     return $self->{'INST_FILE'} = $inst_file if -f $inst_file;
  2622.     $self->SUPER::inst_file;
  2623. }
  2624.  
  2625. sub rematein {
  2626.     my($self,$meth) = @_;
  2627.     $self->debug("self[$self] meth[$meth]") if $CPAN::DEBUG;
  2628.     my($s);
  2629.     for $s ($self->contains) {
  2630.     my($type) = $s =~ m|/| ? 'CPAN::Distribution' :
  2631.         $s =~ m|^Bundle::| ? 'CPAN::Bundle' : 'CPAN::Module';
  2632.     if ($type eq 'CPAN::Distribution') {
  2633.         warn qq{
  2634. The Bundle }.$self->id.qq{ contains
  2635. explicitly a file $s.
  2636. };
  2637.         sleep 3;
  2638.     }
  2639.     $CPAN::META->instance($type,$s)->$meth();
  2640.     }
  2641. }
  2642.  
  2643. sub xs_file {
  2644.     return 0;
  2645. }
  2646.  
  2647. sub force   { shift->rematein('force',@_); }
  2648. sub get     { shift->rematein('get',@_); }
  2649. sub make    { shift->rematein('make',@_); }
  2650. sub test    { shift->rematein('test',@_); }
  2651. sub install { shift->rematein('install',@_); }
  2652. sub clean   { shift->rematein('clean',@_); }
  2653.  
  2654. sub readme  {
  2655.     my($self) = @_;
  2656.     my($file) = $self->cpan_file or print("No File found for bundle ", $self->id, "\n"), return;
  2657.     $self->debug("self[$self] file[$file]") if $CPAN::DEBUG;
  2658.     $CPAN::META->instance('CPAN::Distribution',$file)->readme;
  2659. }
  2660.  
  2661. package CPAN::Module;
  2662.  
  2663. sub as_glimpse {
  2664.     my($self) = @_;
  2665.     my(@m);
  2666.     my $class = ref($self);
  2667.     $class =~ s/^CPAN:://;
  2668.     push @m, sprintf "%-15s %-15s (%s)\n", $class, $self->{ID}, $self->cpan_file;
  2669.     join "", @m;
  2670. }
  2671.  
  2672. sub as_string {
  2673.     my($self) = @_;
  2674.     my(@m);
  2675.     CPAN->debug($self) if $CPAN::DEBUG;
  2676.     my $class = ref($self);
  2677.     $class =~ s/^CPAN:://;
  2678.     local($^W) = 0;
  2679.     push @m, $class, " id = $self->{ID}\n";
  2680.     my $sprintf = "    %-12s %s\n";
  2681.     push @m, sprintf $sprintf, 'DESCRIPTION', $self->{description} if $self->{description};
  2682.     my $sprintf2 = "    %-12s %s (%s)\n";
  2683.     my($userid);
  2684.     if ($userid = $self->{'CPAN_USERID'} || $self->{'userid'}){
  2685.     push @m, sprintf(
  2686.              $sprintf2,
  2687.              'CPAN_USERID',
  2688.              $userid,
  2689.              CPAN::Shell->expand('Author',$userid)->fullname
  2690.             )
  2691.     }
  2692.     push @m, sprintf $sprintf, 'CPAN_VERSION', $self->{CPAN_VERSION} if $self->{CPAN_VERSION};
  2693.     push @m, sprintf $sprintf, 'CPAN_FILE', $self->{CPAN_FILE} if $self->{CPAN_FILE};
  2694.     my $sprintf3 = "    %-12s %1s%1s%1s%1s (%s,%s,%s,%s)\n";
  2695.     my(%statd,%stats,%statl,%stati);
  2696.     @statd{qw,? i c a b R M S,} = qw,unknown idea pre-alpha alpha beta released mature standard,;
  2697.     @stats{qw,? m d u n,}       = qw,unknown mailing-list developer comp.lang.perl.* none,;
  2698.     @statl{qw,? p c + o,}       = qw,unknown perl C C++ other,;
  2699.     @stati{qw,? f r O,}         = qw,unknown functions references+ties object-oriented,;
  2700.     $statd{' '} = 'unknown';
  2701.     $stats{' '} = 'unknown';
  2702.     $statl{' '} = 'unknown';
  2703.     $stati{' '} = 'unknown';
  2704.     push @m, sprintf(
  2705.              $sprintf3,
  2706.              'DSLI_STATUS',
  2707.              $self->{statd},
  2708.              $self->{stats},
  2709.              $self->{statl},
  2710.              $self->{stati},
  2711.              $statd{$self->{statd}},
  2712.              $stats{$self->{stats}},
  2713.              $statl{$self->{statl}},
  2714.              $stati{$self->{stati}}
  2715.             ) if $self->{statd};
  2716.     my $local_file = $self->inst_file;
  2717.     if ($local_file && ! exists $self->{MANPAGE}) {
  2718.     my $fh = FileHandle->new($local_file) or Carp::croak("Couldn't open $local_file: $!");
  2719.     my $inpod = 0;
  2720.     my(@result);
  2721.     local $/ = "\n";
  2722.     while (<$fh>) {
  2723.         $inpod = /^=(?!head1\s+NAME)/ ? 0 : /^=head1\s+NAME/ ? 1 : $inpod;
  2724.         next unless $inpod;
  2725.         next if /^=/;
  2726.         next if /^\s+$/;
  2727.         chomp;
  2728.         push @result, $_;
  2729.     }
  2730.     close $fh;
  2731.     $self->{MANPAGE} = join " ", @result;
  2732.     }
  2733.     my($item);
  2734.     for $item (qw/MANPAGE CONTAINS/) {
  2735.     push @m, sprintf $sprintf, $item, $self->{$item} if exists $self->{$item};
  2736.     }
  2737.     push @m, sprintf $sprintf, 'INST_FILE', $local_file || "(not installed)";
  2738.     push @m, sprintf $sprintf, 'INST_VERSION', $self->inst_version if $local_file;
  2739.     join "", @m, "\n";
  2740. }
  2741.  
  2742. sub cpan_file    {
  2743.     my $self = shift;
  2744.     CPAN->debug($self->id) if $CPAN::DEBUG;
  2745.     unless (defined $self->{'CPAN_FILE'}) {
  2746.     CPAN::Index->reload;
  2747.     }
  2748.     if (defined $self->{'CPAN_FILE'}){
  2749.     return $self->{'CPAN_FILE'};
  2750.     } elsif (defined $self->{'userid'}) {
  2751.     return "Contact Author ".$self->{'userid'}."=".$CPAN::META->instance(CPAN::Author,$self->{'userid'})->fullname
  2752.     } else {
  2753.     return "N/A";
  2754.     }
  2755. }
  2756.  
  2757. *name = \&cpan_file;
  2758.  
  2759. sub cpan_version { shift->{'CPAN_VERSION'} }
  2760.  
  2761. sub force {
  2762.     my($self) = @_;
  2763.     $self->{'force_update'}++;
  2764. }
  2765.  
  2766. sub rematein {
  2767.     my($self,$meth) = @_;
  2768.     $self->debug($self->id) if $CPAN::DEBUG;
  2769.     my $cpan_file = $self->cpan_file;
  2770.     return if $cpan_file eq "N/A";
  2771.     return if $cpan_file =~ /^Contact Author/;
  2772.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  2773.     $pack->called_for($self->id);
  2774.     $pack->force if exists $self->{'force_update'};
  2775.     $pack->$meth();
  2776.     delete $self->{'force_update'};
  2777. }
  2778.  
  2779. sub readme { shift->rematein('readme') }
  2780. sub look { shift->rematein('look') }
  2781. sub get    { shift->rematein('get',@_); }
  2782. sub make   { shift->rematein('make') }
  2783. sub test   { shift->rematein('test') }
  2784. sub install {
  2785.     my($self) = @_;
  2786.     my($doit) = 0;
  2787.     my($latest) = $self->cpan_version;
  2788.     $latest ||= 0;
  2789.     my($inst_file) = $self->inst_file;
  2790.     my($have) = 0;
  2791.     if (defined $inst_file) {
  2792.     $have = $self->inst_version;
  2793.     }
  2794.     if (1){ # A block for scoping $^W, the if is just for the visual
  2795.     local($^W)=0;
  2796.     if ($inst_file && $have >= $latest && not exists $self->{'force_update'}) {
  2797.         print $self->id, " is up to date.\n";
  2798.     } else {
  2799.         $doit = 1;
  2800.     }
  2801.     }
  2802.     $self->rematein('install') if $doit;
  2803. }
  2804. sub clean  { shift->rematein('clean') }
  2805.  
  2806. sub inst_file {
  2807.     my($self) = @_;
  2808.     my($dir,@packpath);
  2809.     @packpath = split /::/, $self->{ID};
  2810.     $packpath[-1] .= ".pm";
  2811.     foreach $dir (@INC) {
  2812.     my $pmfile = CPAN->catfile($dir,@packpath);
  2813.     if (-f $pmfile){
  2814.         return $pmfile;
  2815.     }
  2816.     }
  2817.     return;
  2818. }
  2819.  
  2820. sub xs_file {
  2821.     my($self) = @_;
  2822.     my($dir,@packpath);
  2823.     @packpath = split /::/, $self->{ID};
  2824.     push @packpath, $packpath[-1];
  2825.     $packpath[-1] .= "." . $Config::Config{'dlext'};
  2826.     foreach $dir (@INC) {
  2827.     my $xsfile = CPAN->catfile($dir,'auto',@packpath);
  2828.     if (-f $xsfile){
  2829.         return $xsfile;
  2830.     }
  2831.     }
  2832.     return;
  2833. }
  2834.  
  2835. sub inst_version {
  2836.     my($self) = @_;
  2837.     my $parsefile = $self->inst_file or return 0;
  2838.     local($^W) = 0 if $] < 5.00303 && $ExtUtils::MakeMaker::VERSION < 5.38;
  2839.     my $have = MM->parse_version($parsefile);
  2840.     $have ||= 0;
  2841.     $have =~ s/\s+//g;
  2842.     $have ||= 0;
  2843.     $have;
  2844. }
  2845.  
  2846. package CPAN;
  2847.  
  2848. 1;
  2849.  
  2850. __END__
  2851.  
  2852. =head1 NAME
  2853.  
  2854. CPAN - query, download and build perl modules from CPAN sites
  2855.  
  2856. =head1 SYNOPSIS
  2857.  
  2858. Interactive mode:
  2859.  
  2860.   perl -MCPAN -e shell;
  2861.  
  2862. Batch mode:
  2863.  
  2864.   use CPAN;
  2865.  
  2866.   autobundle, clean, install, make, recompile, test
  2867.  
  2868. =head1 DESCRIPTION
  2869.  
  2870. The CPAN module is designed to automate the make and install of perl
  2871. modules and extensions. It includes some searching capabilities and
  2872. knows how to use Net::FTP or LWP (or lynx or an external ftp client)
  2873. to fetch the raw data from the net.
  2874.  
  2875. Modules are fetched from one or more of the mirrored CPAN
  2876. (Comprehensive Perl Archive Network) sites and unpacked in a dedicated
  2877. directory.
  2878.  
  2879. The CPAN module also supports the concept of named and versioned
  2880. 'bundles' of modules. Bundles simplify the handling of sets of
  2881. related modules. See BUNDLES below.
  2882.  
  2883. The package contains a session manager and a cache manager. There is
  2884. no status retained between sessions. The session manager keeps track
  2885. of what has been fetched, built and installed in the current
  2886. session. The cache manager keeps track of the disk space occupied by
  2887. the make processes and deletes excess space according to a simple FIFO
  2888. mechanism.
  2889.  
  2890. All methods provided are accessible in a programmer style and in an
  2891. interactive shell style.
  2892.  
  2893. =head2 Interactive Mode
  2894.  
  2895. The interactive mode is entered by running
  2896.  
  2897.     perl -MCPAN -e shell
  2898.  
  2899. which puts you into a readline interface. You will have most fun if
  2900. you install Term::ReadKey and Term::ReadLine to enjoy both history and
  2901. completion.
  2902.  
  2903. Once you are on the command line, type 'h' and the rest should be
  2904. self-explanatory.
  2905.  
  2906. The most common uses of the interactive modes are
  2907.  
  2908. =over 2
  2909.  
  2910. =item Searching for authors, bundles, distribution files and modules
  2911.  
  2912. There are corresponding one-letter commands C<a>, C<b>, C<d>, and C<m>
  2913. for each of the four categories and another, C<i> for any of the
  2914. mentioned four. Each of the four entities is implemented as a class
  2915. with slightly differing methods for displaying an object.
  2916.  
  2917. Arguments you pass to these commands are either strings matching exact
  2918. the identification string of an object or regular expressions that are
  2919. then matched case-insensitively against various attributes of the
  2920. objects. The parser recognizes a regualar expression only if you
  2921. enclose it between two slashes.
  2922.  
  2923. The principle is that the number of found objects influences how an
  2924. item is displayed. If the search finds one item, we display the result
  2925. of object-E<gt>as_string, but if we find more than one, we display
  2926. each as object-E<gt>as_glimpse. E.g.
  2927.  
  2928.     cpan> a ANDK
  2929.     Author id = ANDK
  2930.     EMAIL        a.koenig@franz.ww.TU-Berlin.DE
  2931.     FULLNAME     Andreas K÷nig
  2932.  
  2933.  
  2934.     cpan> a /andk/
  2935.     Author id = ANDK
  2936.     EMAIL        a.koenig@franz.ww.TU-Berlin.DE
  2937.     FULLNAME     Andreas K÷nig
  2938.  
  2939.  
  2940.     cpan> a /and.*rt/
  2941.     Author          ANDYD (Andy Dougherty)
  2942.     Author          MERLYN (Randal L. Schwartz)
  2943.  
  2944. =item make, test, install, clean  modules or distributions
  2945.  
  2946. These commands do indeed exist just as written above. Each of them
  2947. takes any number of arguments and investigates for each what it might
  2948. be. Is it a distribution file (recognized by embedded slashes), this
  2949. file is being processed. Is it a module, CPAN determines the
  2950. distribution file where this module is included and processes that.
  2951.  
  2952. Any C<make>, C<test>, and C<readme> are run unconditionally. A
  2953.  
  2954.   install <distribution_file>
  2955.  
  2956. also is run unconditionally.  But for
  2957.  
  2958.   install <module>
  2959.  
  2960. CPAN checks if an install is actually needed for it and prints
  2961. I<Foo up to date> in case the module doesnE<39>t need to be updated.
  2962.  
  2963. CPAN also keeps track of what it has done within the current session
  2964. and doesnE<39>t try to build a package a second time regardless if it
  2965. succeeded or not. The C<force > command takes as first argument the
  2966. method to invoke (currently: make, test, or install) and executes the
  2967. command from scratch.
  2968.  
  2969. Example:
  2970.  
  2971.     cpan> install OpenGL
  2972.     OpenGL is up to date.
  2973.     cpan> force install OpenGL
  2974.     Running make
  2975.     OpenGL-0.4/
  2976.     OpenGL-0.4/COPYRIGHT
  2977.     [...]
  2978.  
  2979. =item readme, look module or distribution
  2980.  
  2981. These two commands take only one argument, be it a module or a
  2982. distribution file. C<readme> displays the README of the associated
  2983. distribution file. C<Look> gets and untars (if not yet done) the
  2984. distribution file, changes to the appropriate directory and opens a
  2985. subshell process in that directory.
  2986.  
  2987. =back
  2988.  
  2989. =head2 CPAN::Shell
  2990.  
  2991. The commands that are available in the shell interface are methods in
  2992. the package CPAN::Shell. If you enter the shell command, all your
  2993. input is split by the Text::ParseWords::shellwords() routine which
  2994. acts like most shells do. The first word is being interpreted as the
  2995. method to be called and the rest of the words are treated as arguments
  2996. to this method.
  2997.  
  2998. =head2 autobundle
  2999.  
  3000. C<autobundle> writes a bundle file into the
  3001. C<$CPAN::Config-E<gt>{cpan_home}/Bundle> directory. The file contains
  3002. a list of all modules that are both available from CPAN and currently
  3003. installed within @INC. The name of the bundle file is based on the
  3004. current date and a counter.
  3005.  
  3006. =head2 recompile
  3007.  
  3008. recompile() is a very special command in that it takes no argument and
  3009. runs the make/test/install cycle with brute force over all installed
  3010. dynamically loadable extensions (aka XS modules) with 'force' in
  3011. effect. Primary purpose of this command is to finish a network
  3012. installation. Imagine, you have a common source tree for two different
  3013. architectures. You decide to do a completely independent fresh
  3014. installation. You start on one architecture with the help of a Bundle
  3015. file produced earlier. CPAN installs the whole Bundle for you, but
  3016. when you try to repeat the job on the second architecture, CPAN
  3017. responds with a C<"Foo up to date"> message for all modules. So you
  3018. will be glad to run recompile in the second architecture and
  3019. youE<39>re done.
  3020.  
  3021. Another popular use for C<recompile> is to act as a rescue in case your
  3022. perl breaks binary compatibility. If one of the modules that CPAN uses
  3023. is in turn depending on binary compatibility (so you cannot run CPAN
  3024. commands), then you should try the CPAN::Nox module for recovery.
  3025.  
  3026. =head2 The 4 C<CPAN::*> Classes: Author, Bundle, Module, Distribution
  3027.  
  3028. Although it may be considered internal, the class hierarchie does
  3029. matter for both users and programmer. CPAN.pm deals with above
  3030. mentioned four classes, and all those classes share a set of
  3031. methods. It is a classical single polymorphism that is in effect.  A
  3032. metaclass object registers all objects of all kinds and indexes them
  3033. with a string. The strings referencing objects have a separated
  3034. namespace (well, not completely separated):
  3035.  
  3036.          Namespace                         Class
  3037.  
  3038.    words containing a "/" (slash)      Distribution
  3039.     words starting with Bundle::          Bundle
  3040.           everything else            Module or Author
  3041.  
  3042. Modules know their associated Distribution objects. They always refer
  3043. to the most recent official release. Developers may mark their
  3044. releases as unstable development versions (by inserting an underbar
  3045. into the visible version number), so not always is the default
  3046. distribution for a given module the really hottest and newest. If a
  3047. module Foo circulates on CPAN in both version 1.23 and 1.23_90,
  3048. CPAN.pm offers a convenient way to install version 1.23 by saying
  3049.  
  3050.     install Foo
  3051.  
  3052. This would install the complete distribution file (say
  3053. BAR/Foo-1.23.tar.gz) with all accompanying material in there. But if
  3054. you would like to install version 1.23_90, you need to know where the
  3055. distribution file resides on CPAN relative to the authors/id/
  3056. directory. If the author is BAR, this might be BAR/Foo-1.23_90.tar.gz,
  3057. so he would have to say
  3058.  
  3059.     install BAR/Foo-1.23_90.tar.gz
  3060.  
  3061. The first example will be driven by an object of the class
  3062. CPAN::Module, the second by an object of class Distribution.
  3063.  
  3064. =head2 ProgrammerE<39>s interface
  3065.  
  3066. If you do not enter the shell, the available shell commands are both
  3067. available as methods (C<CPAN::Shell-E<gt>install(...)>) and as
  3068. functions in the calling package (C<install(...)>).
  3069.  
  3070. There's currently only one class that has a stable interface,
  3071. CPAN::Shell. All commands that are available in the CPAN shell are
  3072. methods of the class CPAN::Shell. Each of the commands that produce
  3073. listings of modules (C<r>, C<autobundle>, C<u>) returns a list of the
  3074. IDs of all modules within the list.
  3075.  
  3076. =over 2
  3077.  
  3078. =item expand($type,@things)
  3079.  
  3080. The IDs of all objects available within a program are strings that can
  3081. be expanded to the corresponding real objects with the
  3082. C<CPAN::Shell-E<gt>expand("Module",@things)> method. Expand returns a
  3083. list of CPAN::Module objects according to the C<@things> arguments
  3084. given. In scalar context it only returns the first element of the
  3085. list.
  3086.  
  3087. =item Programming Examples
  3088.  
  3089. This enables the programmer to do operations that combine
  3090. functionalities that are available in the shell.
  3091.  
  3092.     perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
  3093.  
  3094.     for $mod (qw(Net::FTP MD5 Data::Dumper)){
  3095.         my $obj = CPAN::Shell->expand('Module',$mod);
  3096.         $obj->install;
  3097.     }
  3098.  
  3099.     for $mod (CPAN::Shell->expand("Module","/./")){
  3100.     next unless $mod->inst_file;
  3101.     next if $mod->inst_version;
  3102.     print "No VERSION in ", $mod->id, "\n";
  3103.     }
  3104.  
  3105. =back
  3106.  
  3107. =head2 Methods in the four
  3108.  
  3109. =head2 Cache Manager
  3110.  
  3111. Currently the cache manager only keeps track of the build directory
  3112. ($CPAN::Config->{build_dir}). It is a simple FIFO mechanism that
  3113. deletes complete directories below C<build_dir> as soon as the size of
  3114. all directories there gets bigger than $CPAN::Config->{build_cache}
  3115. (in MB). The contents of this cache may be used for later
  3116. re-installations that you intend to do manually, but will never be
  3117. trusted by CPAN itself. This is due to the fact that the user might
  3118. use these directories for building modules on different architectures.
  3119.  
  3120. There is another directory ($CPAN::Config->{keep_source_where}) where
  3121. the original distribution files are kept. This directory is not
  3122. covered by the cache manager and must be controlled by the user. If
  3123. you choose to have the same directory as build_dir and as
  3124. keep_source_where directory, then your sources will be deleted with
  3125. the same fifo mechanism.
  3126.  
  3127. =head2 Bundles
  3128.  
  3129. A bundle is just a perl module in the namespace Bundle:: that does not
  3130. define any functions or methods. It usually only contains documentation.
  3131.  
  3132. It starts like a perl module with a package declaration and a $VERSION
  3133. variable. After that the pod section looks like any other pod with the
  3134. only difference, that I<one special pod section> exists starting with
  3135. (verbatim):
  3136.  
  3137.     =head1 CONTENTS
  3138.  
  3139. In this pod section each line obeys the format
  3140.  
  3141.         Module_Name [Version_String] [- optional text]
  3142.  
  3143. The only required part is the first field, the name of a module
  3144. (eg. Foo::Bar, ie. I<not> the name of the distribution file). The rest
  3145. of the line is optional. The comment part is delimited by a dash just
  3146. as in the man page header.
  3147.  
  3148. The distribution of a bundle should follow the same convention as
  3149. other distributions.
  3150.  
  3151. Bundles are treated specially in the CPAN package. If you say 'install
  3152. Bundle::Tkkit' (assuming such a bundle exists), CPAN will install all
  3153. the modules in the CONTENTS section of the pod.  You can install your
  3154. own Bundles locally by placing a conformant Bundle file somewhere into
  3155. your @INC path. The autobundle() command which is available in the
  3156. shell interface does that for you by including all currently installed
  3157. modules in a snapshot bundle file.
  3158.  
  3159. There is a meaningless Bundle::Demo available on CPAN. Try to install
  3160. it, it usually does no harm, just demonstrates what the Bundle
  3161. interface looks like.
  3162.  
  3163. =head2 Prerequisites
  3164.  
  3165. If you have a local mirror of CPAN and can access all files with
  3166. "file:" URLs, then you only need a perl better than perl5.003 to run
  3167. this module. Otherwise Net::FTP is strongly recommended. LWP may be
  3168. required for non-UNIX systems or if your nearest CPAN site is
  3169. associated with an URL that is not C<ftp:>.
  3170.  
  3171. If you have neither Net::FTP nor LWP, there is a fallback mechanism
  3172. implemented for an external ftp command or for an external lynx
  3173. command.
  3174.  
  3175. This module presumes that all packages on CPAN
  3176.  
  3177. =over 2
  3178.  
  3179. =item *
  3180.  
  3181. declare their $VERSION variable in an easy to parse manner. This
  3182. prerequisite can hardly be relaxed because it consumes by far too much
  3183. memory to load all packages into the running program just to determine
  3184. the $VERSION variable . Currently all programs that are dealing with
  3185. version use something like this
  3186.  
  3187.     perl -MExtUtils::MakeMaker -le \
  3188.         'print MM->parse_version($ARGV[0])' filename
  3189.  
  3190. If you are author of a package and wonder if your $VERSION can be
  3191. parsed, please try the above method.
  3192.  
  3193. =item *
  3194.  
  3195. come as compressed or gzipped tarfiles or as zip files and contain a
  3196. Makefile.PL (well we try to handle a bit more, but without much
  3197. enthusiasm).
  3198.  
  3199. =back
  3200.  
  3201. =head2 Debugging
  3202.  
  3203. The debugging of this module is pretty difficult, because we have
  3204. interferences of the software producing the indices on CPAN, of the
  3205. mirroring process on CPAN, of packaging, of configuration, of
  3206. synchronicity, and of bugs within CPAN.pm.
  3207.  
  3208. In interactive mode you can try "o debug" which will list options for
  3209. debugging the various parts of the package. The output may not be very
  3210. useful for you as it's just a byproduct of my own testing, but if you
  3211. have an idea which part of the package may have a bug, it's sometimes
  3212. worth to give it a try and send me more specific output. You should
  3213. know that "o debug" has built-in completion support.
  3214.  
  3215. =head2 Floppy, Zip, and all that Jazz
  3216.  
  3217. CPAN.pm works nicely without network too. If you maintain machines
  3218. that are not networked at all, you should consider working with file:
  3219. URLs. Of course, you have to collect your modules somewhere first. So
  3220. you might use CPAN.pm to put together all you need on a networked
  3221. machine. Then copy the $CPAN::Config->{keep_source_where} (but not
  3222. $CPAN::Config->{build_dir}) directory on a floppy. This floppy is kind
  3223. of a personal CPAN. CPAN.pm on the non-networked machines works nicely
  3224. with this floppy.
  3225.  
  3226. =head1 CONFIGURATION
  3227.  
  3228. When the CPAN module is installed a site wide configuration file is
  3229. created as CPAN/Config.pm. The default values defined there can be
  3230. overridden in another configuration file: CPAN/MyConfig.pm. You can
  3231. store this file in $HOME/.cpan/CPAN/MyConfig.pm if you want, because
  3232. $HOME/.cpan is added to the search path of the CPAN module before the
  3233. use() or require() statements.
  3234.  
  3235. Currently the following keys in the hash reference $CPAN::Config are
  3236. defined:
  3237.  
  3238.   build_cache        size of cache for directories to build modules
  3239.   build_dir          locally accessible directory to build modules
  3240.   index_expire       after how many days refetch index files
  3241.   cpan_home          local directory reserved for this package
  3242.   gzip             location of external program gzip
  3243.   inactivity_timeout breaks interactive Makefile.PLs after that
  3244.                      many seconds inactivity. Set to 0 to never break.
  3245.   inhibit_startup_message
  3246.                      if true, does not print the startup message
  3247.   keep_source        keep the source in a local directory?
  3248.   keep_source_where  where keep the source (if we do)
  3249.   make               location of external program make
  3250.   make_arg         arguments that should always be passed to 'make'
  3251.   make_install_arg   same as make_arg for 'make install'
  3252.   makepl_arg         arguments passed to 'perl Makefile.PL'
  3253.   pager              location of external program more (or any pager)
  3254.   tar                location of external program tar
  3255.   unzip              location of external program unzip
  3256.   urllist         arrayref to nearby CPAN sites (or equivalent locations)
  3257.  
  3258. You can set and query each of these options interactively in the cpan
  3259. shell with the command set defined within the C<o conf> command:
  3260.  
  3261. =over 2
  3262.  
  3263. =item o conf E<lt>scalar optionE<gt>
  3264.  
  3265. prints the current value of the I<scalar option>
  3266.  
  3267. =item o conf E<lt>scalar optionE<gt> E<lt>valueE<gt>
  3268.  
  3269. Sets the value of the I<scalar option> to I<value>
  3270.  
  3271. =item o conf E<lt>list optionE<gt>
  3272.  
  3273. prints the current value of the I<list option> in MakeMaker's
  3274. neatvalue format.
  3275.  
  3276. =item o conf E<lt>list optionE<gt> [shift|pop]
  3277.  
  3278. shifts or pops the array in the I<list option> variable
  3279.  
  3280. =item o conf E<lt>list optionE<gt> [unshift|push|splice] E<lt>listE<gt>
  3281.  
  3282. works like the corresponding perl commands.
  3283.  
  3284. =back
  3285.  
  3286. =head1 SECURITY
  3287.  
  3288. There's no strong security layer in CPAN.pm. CPAN.pm helps you to
  3289. install foreign, unmasked, unsigned code on your machine. We compare
  3290. to a checksum that comes from the net just as the distribution file
  3291. itself. If somebody has managed to tamper with the distribution file,
  3292. they may have as well tampered with the CHECKSUMS file. Future
  3293. development will go towards strong authentification.
  3294.  
  3295. =head1 EXPORT
  3296.  
  3297. Most functions in package CPAN are exported per default. The reason
  3298. for this is that the primary use is intended for the cpan shell or for
  3299. oneliners.
  3300.  
  3301. =head1 BUGS
  3302.  
  3303. we should give coverage for _all_ of the CPAN and not just the
  3304. __PAUSE__ part, right? In this discussion CPAN and PAUSE have become
  3305. equal -- but they are not. PAUSE is authors/ and modules/. CPAN is
  3306. PAUSE plus the clpa/, doc/, misc/, ports/, src/, scripts/.
  3307.  
  3308. Future development should be directed towards a better intergration of
  3309. the other parts.
  3310.  
  3311. =head1 AUTHOR
  3312.  
  3313. Andreas K÷nig E<lt>a.koenig@mind.deE<gt>
  3314.  
  3315. =head1 SEE ALSO
  3316.  
  3317. perl(1), CPAN::Nox(3)
  3318.  
  3319. =cut
  3320.  
  3321.